Data Binding Expression Syntax

Data-binding expressions create bindings between a server control property and a data source when the DataBind method 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 an XmlDataSource control using the XPath and XPathSelect methods, as well as the XPathBinder class. For more information, see XmlDataSource Web Server Control.

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 the DropDownList Web server control, the Label Web server control is bound against the selected item in the list and displays the selected state.

    <script language="C#" runat="server">
        void SubmitBtn_Click(Object sender, EventArgs e) {
          // 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.   

    <h3><font face="Verdana">Binding to a property of another server control</font></h3>
    <form runat="server">
        <asp:DropDownList id="StateList" runat="server">
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>        
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>     

Community Additions