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 want to bind the TextBox Web server control to some data. You create a data binding expression and assign it to the Text attribute of the control so that the value is displayed in the control.
The following example shows the general form of the control declaration in the HTML view. The Text property of the control is bound to the data view that contains a single record. The data binding expression is separated by characters <% # and %>. <Asp: TextBox id = "TextBox1" runat = "server"
Text = '<% # DataView1 (0) ("au_lname") %>'>
</Asp: TextBox>
Similarly, you can use a data binding expression to set the ImageUrl attribute of the Image 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:
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.
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 use the DataBinder class by calling its Eval method, which requires two parameters:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.