Wednesday, October 29, 2008

ASP.Net - When to use Eval() and Bind() during DataBinding

Eval is used to bind to UI item that is setup to be read-only (eg: a label or a read-only text box), i.e., Eval is used for one way binding - for reading from a database into a UI field.

Bind is used to bind to UI item that is setup to display as well as edit values (a text-box that is not read-only, combo-box, etc). Thus Bind is used for 2-way binding - for displaying data from the database as well as saving data to the database.


An interesting tidbit: The Eval method is found in the TemplateControl class. The Bind method, is not a true method in ASP.Net. It is used as a compiler directive, where the compiler upon encountering a Bind method will actually generate an Eval statement for you as well as another statement that pulls the value out of the control and provides it for populating to a database.

This is the reason that you cannot use arbitrary methods in your Bind (or surrounding your Bind call).

Bet you did not know that!

more info:

No comments: