Поделиться через


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 %>

Parameters

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

Remarks

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.

Example

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.

<html>
<head>
    <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.   
          Page.DataBind();
        }
    </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>
<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>

See Also

Reference

XPathBinder

Concepts

Introduction to ASP.NET Web Pages
ASP.NET Web Page Syntax Overview

Other Resources

XmlDataSource Web Server Control