Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ASP.NET Reference
ASP.NET Page Syntax
 Data-Binding Expression Syntax
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework General Reference
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 %>
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.

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

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.

C#
<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>
Visual Basic
<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>
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
See an example of how to Bind database data to server controls      CSharpUniversity.com   |   Edit   |   Show History
The ability to bind database data to server controls like the TextBox is very useful. You can support read and write operations. Visit the following articles to see comprehensive examples:

http://www.csharpuniversity.com/2009/02/09/binding-database-data-to-textbox-with-the-formview-and-aspnet-sqldatasource-controls/

http://www.csharpuniversity.com/2009/02/09/updating-database-data-using-parameters-from-textbox-controls-in-a-formview-with-the-aspnet-sqldatasource-control/
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker