You can bind any single-value Web server control property — for example, the Text property of a TextBox or the ImageUrl property of an Image control — to a data value. This includes not only display properties, but behavior properties such as size, width, and font.
NOTE: If you are working with list controls ( Repeater, DataList, DataGrid, CheckBoxList, and RadioButtonList ), you specify data binding for the control as a whole using special data-binding properties. For details, see Data Binding Multiple Record Controls. However, you can bind their single-value properties such as size and font using the procedure in this topic.
You specify single-value data binding by creating a data-binding expression. When the control performs data binding, it resolves the expression and assigns the resulting value to the specified property. You can data bind as many properties on a control as you need.
NOTE: If you are working with HTML server controls ( such as the HtmlInputText or HtmlSelect control ), data-binding procedures are different. For details, see Data Binding HTML Server Controls.
- Create a data-binding expression and assign it as the value for a property ( attribute ) . The expression’s syntax is:
<%# expression %>
The following example shows a data-binding expression. The expression gets the value of the au_lname
column from the first record of a data view and assigns it to the Text property of a TextBox Web control.
<asp:TextBox id="myTextBox" runat="server"
Text='<%# DataView1 ( 0 ) ( "au_lname" ) %
>'>
</asp:TextBox>
NOTE: For more information, see Data Binding Expressions for Web Forms Pages and Data Binding Expression Syntax.
- Add code to the Web Forms page to call the control’s or the page’s DataBind method.
NOTE: You generally do not want to call the DataBind method on each round trip ( that is, in the page initialization without checking for a post back ), because doing so replaces the values in controls.