ASP.NET Web Forms Data Binding in Web Forms
Data binding for HTML server controls for example, the HtmlButton and HtmlInputText controls works slightly differently than it does Web server controls such as the DataList, DataGrid, and TextBox controls.
- Set the property to bind to a data-binding expression. For details, see Data Binding Expressions for Web Forms Pages. The expression should resolve to a single value.
Be sure to put quotation marks around the expression.
The following example shows what an HtmlInputText control might look like after you have bound it to an author's name. The binding assumes that the page contains a data view called DataView1 that displays information from an authors table. The expression gets the au_lname value from the first ( number zero ) record returned by the data view.
<input id="Text1"
type="text"
name="Text1"
runat="server"
value='<%# DataBinder.Eval ( DataView1, " [ 0 ].au_lname" ) %>'
>
Because the HtmlSelect server control ( the HTML Listbox and Dropdown controls ) can display multiple records, it supports special data-binding properties and the data-binding procedure is slightly different.
- Set the control's DataSource property to a data-binding expression that resolves to multiple values. For details, see Data Binding Expressions for Web Forms Pages. A typical value is the name of a dataset instance on your page.
TIP: Be sure to put quotation marks around the values.
- If the data source contains multiple members ( for example, if you are binding to a dataset with multiple tables in it ), set the control's DataMember property to the name of the member to use.
- If the member contains multiple fields, set the control's DataTextField property to the name of the field to display. For example, this is often the name of a column in a data table.
- If you want to return a different value than what the user selects ( for example, the control displays names but you want to return ID numbers ), set the control's DataValueField property.
The following example shows what an HtmlSelect control might look like in HTML view after you have bound it to the authors table in a dataset instance called DsAuthors1. The control displays the value of the au_lname column and when the user makes a selection, it returns the value of the au_id column.
<select id="select1" size="2" name="select1" runat="server"
datasource="<%# dsauthors1 %>"
datamember="authors"
datatextfield="au_lname"
datavaluefield="au_id">
<option></option>
</select>
|
|