Data Binding Expression Syntax
Data-binding expressions create bindings between a server control property and a data source when themethod is called on the page. You can include data-binding expressions on the value side of an attribute/value pair in the opening tag of a server control, or anywhere in the page.
<tagprefix:tagname property="<%# data-binding expression %>" runat="server" /> - or - literal text <%# data-binding expression %>
The control property for which this data binding is declared.
- data-binding expression
Any expression that conforms to the requirements outlined in the Remarks section.
All data-binding expressions must be contained between <%# and %> characters.
ASP.NET supports a hierarchical data-binding model that creates bindings between server control properties and data sources. Almost any server control property can be bound against any public field or property on the containing page or on the server control's immediate naming container.
Data-binding expressions use the Eval and Bind methods to bind data to controls and submit changes back to the database. The Eval method is a static (read-only) method that takes the value of a data field and returns it as a string. The Bind method supports read/write functionality with the ability to retrieve the values of data-bound controls and submit any changes made back to the database.
You can bind to XML data from ancontrol using the XPath and XPathSelect methods, as well as the class. For more information, see .
The following code example demonstrates how you can bind data against properties in an ASP.NET server control. When a user selects a state from theWeb server control, the Web server control is bound against the selected item in the list and displays the selected state.
<html> <head> <script language="VB" runat="server"> Sub SubmitBtn_Click(sender As Object, e As EventArgs) ' Rather than explictly pulling out the variable from the StateList control ' and then manipulating a Label control, just call Page.DataBind. ' This will evaluate any <%# %> expressions within the page. Page.DataBind() End Sub </script> </head> <body> <h3><font face="Verdana"> Binding to a property of another server control</font></h3> <form runat="server"> <asp:DropDownList id="StateList" runat="server"> <asp:ListItem>CA</asp:ListItem> <asp:ListItem>IN</asp:ListItem> <asp:ListItem>KS</asp:ListItem> <asp:ListItem>MD</asp:ListItem> <asp:ListItem>MI</asp:ListItem> <asp:ListItem>OR</asp:ListItem> <asp:ListItem>TN</asp:ListItem> <asp:ListItem>UT</asp:ListItem> </asp:DropDownList> <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/> <p> Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/> </form> </body> </html>