Wednesday, June 3, 2009

What is the difference between Eval and Bind in ASP 2.0

Eval function is used to define one-way (read-only) binding - used for read-only values such as Labels, Literals

Eval method belongs to the TemplateControl class and has to override methods TemplateControl.Eval(field_name) and TemplateControl.Eval(field_name, format)Bind function is used for two-way (updatable) binding - used for read-write values such as TextBoxes

There isn’t a bind method in ASP.NET! When ASP.NET parses your file and sees you're using a databinding expression (in the angle-bracket-percent-pound format, "<%# %>") it has special-case code to parse for the Bind syntax and generates some special code for it. When you use <%# Bind("Name") %> it's not a real function call.

If ASP.NET parses the code and detects a Bind() statement, it splits the statement into two parts.

1) The first part is the one-way databinding portion, which ends up being just a regular Eval() call.
2) The second part is the reverse portion, which is typically some code along the lines of "string name = TextBox1.Text" that grabs the value back out from where it was bound.

The only formats supported in two-way databinding are Bind("field") and Bind("field", "format string {0}"). There are some very minor variations of these syntax examples, such as allowing use of single quotes and not just double quotes.

No comments:

Post a Comment

Locations of visitors to this page