aspxtreme

Data Binding HTML Server Controls

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.

To data bind an HTML server control

  • 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.

To data bind an HtmlSelect server control

  1. 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.

  2. 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.
  3. 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.
  4. 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>


Books and more ...


Suggested Reading

Need a break ?



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme