asp.net.ph

Skip Navigation LinksHome > ASP.NET Web Forms > Data Binding in Web Forms > Data Binding HTML Server Controls

Data Binding HTML Server Controls


Data binding for HTML server controls works as it does for Web server controls, the only diference is in the names of the attributes and properties asociated with HTML controls.

To Bind a Single Value to an HTML Control

While not commonly used, here we look at an example of using databinding to bind a property of an HTML control with a value or expression from another HTML control on the page, using the same syntax as for ASP.NET controls.

<controlname propertyname="<%# databinding expression %>" runat="server" />

The below code snippet demonstrates this scenario. When a user selects a value from the HtmlSelect control, the HtmlInput control binds against the selected item in the <select> list.

<select id="lstStates" runat="server"
   datatextfield="statename" />

<button type="submit" onServerClick="getState" runat="server">Send</button>

<p>Selected State: <input value='<%# lstStates.Value %>' runat="server" />
Data Binding HTML Server Controls
Run Sample | View Source

Rather than explictly pull out the variable from the <select> and then manipulate the <input> control, we just call Page.DataBind instead. This evaluates any <%# ... %> expressions within the page.

To Bind Multiple Values to an HTML Control

This example shows how databinding is used to dynamically generate hyperlinks on a Web Forms control, using values from a given field in a data source.

When databinding is used within templates in list controls such as the <asp:Repeater>, <asp:DataGrid>, <asp:DataList>, and <asp:GridView>, only a single declaration is needed to generate each hyperlink item in the list.

Note that the method used in this sample not only generates the link text, but also the querystring parameter of the url, which determines what argument is passed along to each link.

DataBinding to the HtmlAnchor Control
Run Sample | View Source
See Also



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

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