Binding data to various control attributes on the web forms page is not achieved by directly binding the attributes to the data source. Instead, data is bound using a special expression format. Information related to the data to be bound is placed in the expression, and the result of the expression is allocated to the control property.
For example, assume that you wantTextboxThe Web server control is bound to some data. You create a data binding expression and assign it toTextProperty so that the value is displayed in the control.
The following example shows the general form of the control declaration in the HTML view. ControlTextProperty is bound to a data view that contains a single record. The data binding expression is a character. <%#
And %>
.
<asp:TextBox id="TextBox1" runat="server" Text='<%# DataView1(0)("au_lname") %>
'></asp:TextBox>
Similarly, you can use a data binding expression to setImageThe imageurl attribute of the Web server control. In this case, you extract a string from the database that contains the path and file name of the image to be displayed. An example may be similar to the following:
<asp:Image id=Image1 runat="server" ImageUrl='<%# DataView(0)("productPhotoURL")
%>'>
In Visual Studio, the "properties" window provides you with a tool to create a data binding expression. You can also create your own binding expressions and enter them in the HTML view of the web forms designer.
Advantages of using a data binding expression
Using a data binding expression provides you with flexibility in the following aspects:
1. Any expression can be used, as long as the expression is parsed as a value that can be used by the control. The most common is that the data binding expression is resolved to the value exported from the data source, however, it can also reference the properties of this page or other controls, the value you calculated during running, or almost any other item.
2. The expression can be assigned to any attribute, that is, any attribute can be bound to Data. For example, you can retain information related to user preferences in the database and bind data to fonts, colors, sizes, styles, and other attributes to achieve those preferences. In addition, you can bind more than one control property. This allows you to bind one property to a data source and another property to different sources.
Use the databinder class for binding
Although you can actually bind data using any expression parsed as a value, in most cases, you are bound to some types of data sources. The most common case is a table in a dataset or data view. A table contains a single record that you are interested in. To simplify data binding of this type, the ASP. NET Server Control supports a class named databinder, which extracts data and enables it to be used for control attributes.
You can callEvalMethod to useDatabinderClass, which requires two parameters:
1. References to data containers (usually datasets), data tables, or data views.
2. Reference of the individual values to be exported. This usually references the values of a single row (zero rows) and columns in the row.
The following example shows how to bind the same data as the preceding text box.DatabinderClass.
<asp:TextBox id="TextBox1" runat="server" Text='<%# DataBinder.Eval(DataView1, "[0].au_lname") %>
'></asp:TextBox>
Previous settingsImageWidgetImageurlThe property example may be similar to the following. In this example, a format setting expression isDatabinder. EvalThe second (optional) parameter of the method. This expression adds a path to the data as a prefix.
<asp:Image id=Image1 runat="server" ImageUrl='<%# DataBinder.Eval(Container, "DataItem.ProductImage", "http://myserver/myapps/images/{0}") %>'>
UseDatabinderClass advantages:
1. The syntax is consistent for all bindings.EvalThe parameters required by the method are mandatory.
2. Support for Visual Studio design tools on web forms pagesDatabinderClass.
3. Class automatically performs type conversion. For example, if you bind a text box to a data column containing integers,DatabinderClass automatically converts Integers to strings.
4. You can specify a format expression that can be converted or corrected.
Parse data binding expression
To provide a value that can be bound to a control, the data binding expression must be parsed at runtime. You can explicitly perform this step during page processing by calling the databind method (which is a method of the system. Web. UI. control class. You can call this method for a separate control, or, more effectivelyPageClass (also fromControlClass export) to call this method. This method cascade calls to all child controls. Therefore, you can call this method once for all controls on this page.
GenerallyDatabindMethod:
1. This page is run for the first time, but after filling in the data source (for example, after you have filled the dataset ).
2. After the data source is changed (for example, because records in the data source have been updated ).
The following example shows how to callDatabindTypical method:
' Visual BasicPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DsAuthors1, "authors") If Not (Me.IsPostBack) Then Me.DataBind() End IfEnd Sub// C#private void Page_Load(object sender, System.EventArgs e){ SqlDataAdapter1.Fill(dsAuthors1, "authors"); if (!this.IsPostBack) { this.DataBind(); }}
Generally, you do not need to callDatabindMethod (that is, you do not need to check for sending back in page initialization), because this will replace the value in the control. For example, if you useDataGridControl, which may contain the changes you want to process. When you callDatabindUse values from the data source to replace the grid content. If you perform this operation during page initialization, you will lose the changes in the mesh before you have the chance to process the changes. You should callDatabindMethod.
Source: http://blog.csdn.net/kingwkb/archive/2005/08/05/446406.aspx