.NET Software Development Stack: ASP.NET, WPF, WinForms, ADO.NET, Entity Framework, SQL, Design Patterns
Friday, July 4, 2008
What's the difference between Bind and Eval
Data-binding expressions are resolved when the DataBind method of a control or of the Page class is called. For controls such as the GridView, DetailsView, and FormView controls, data-binding expressions are resolved automatically during the control's PreRender event and you are not required to call the DataBind method explicitly.
Using the Eval Method
The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval method of the DataBinder object, referencing the current data item of the naming container. The naming container is generally the smallest part of the data-bound control that contains a whole record, such as a row in a GridView control. You can therefore use the Eval method only for binding inside templates of a data-bound control.
The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.
Using the Bind Method
The Bind method has some similarities to the Eval method, but there are significant differences. Although you can retrieve the values of data-bound fields with the Bind method, as you can with the Eval method, the Bind method is also used when data can be modified.
In ASP.NET, data-bound controls such as the GridView, DetailsView, and FormView controls can automatically use the update, delete, and insert operations of a data source control. For example, if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a GridView, DetailsView, or FormView control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database. For this reason, the Bind function is used inside the EditItemTemplate or InsertItemTemplate of a data-bound control.
The Bind method is typically used with input controls such as the TextBox control rendered by a GridView row in edit mode. When the data-bound control creates these input controls as part of its own rendering, it can extract the input values.
The Bind method takes the name of a data field to associate with the bound property.
Wednesday, July 2, 2008
More information about ASP.NET Page life-cycle and common events
I’ve found wonderful document about ASP.NET Page lifecycle events on http://john-sheehan.com/blog. Little information about general page Life-cycle stages and events:
The whole document can be found here: http://john-sheehan.com/blog/wp-content/uploads/aspnet-life-cycles-events.pdf . There is also information about Data Binding Events for Data-Bound Controls and Common Life-cycle Events.
Enjoy :)
Sunday, June 29, 2008
ASP.NET data binding essentials
<%# %> Syntax
ASP.NET introduces a new declarative syntax, <%# %>. This syntax is the basis for using data binding in an .aspx page. All data binding expressions must be contained within these characters. The following list includes examples of simple data binding from multiple sources:
• Simple property (syntax for a customer):
<%# custID %>
• Collection (syntax for an order):
<asp:listbox id="List1" datasource="'<%#">' runat="server">
• Expression (syntax for a contact):
<%# ( customer.First Name + " " + customer.LastName ) %>
• Method result (syntax for the outstanding balance):
<%# GetBalance(custID) %>
In the preceding examples, the inline <%# %> tags indicate where the information from a specific data source is to be placed in the .aspx page. The following data binding example uses a TextBox Web server control: <asp:textbox id="txt" text=="<%# custID %>" runat=server />
For more information about data binding syntax, see the following .NET Framework Software Development Kit (SDK) documentation:
Data Binding Expression Syntax
http://msdn2.microsoft.com/en-us/library/bda9bbfx(vs.71).aspx