Data Binding Expression Syntax
Data-binding expressions create bindings between any property on an ASP.NET page, including 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 %>
Attributes
- property
The control property for which this data binding is declared. - data-binding expression
Any expression that conforms to the requirements outlined in the following Remarks section.
Remarks
All data-binding expressions, regardless of where you place them, must be contained between <%# and %> characters.
ASP.NET supports a hierarchical data-binding model that supports associative bindings between server control properties and parent data sources. Any server control property can be data bound against any public field or property on the containing page or on the server control's immediate naming container.
Using DataBinder.Eval
The ASP.NET supplies a static method, called DataBinder.Eval, that evaluates late-bound data-binding expressions and optionally formats the result as a string. This method eliminates much of the explicit casting you must do to coerce values to the data type you want.
In the following code fragment, for example, an integer is displayed as a currency string. With the standard ASP.NET data-binding syntax, you must first cast the type of the data row in order to retrieve the data field, IntegerValue
. Next, this is passed as an argument to the String.Format method:
<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>
Contrast this syntax with that of DataBinder.Eval, which has only three arguments: the naming container for the data item, the data field name, and a format string. In a templated list like DataList Class, DataGrid Class, or Repeater Class, the naming container is always Container.DataItem.
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
The format string argument is optional. If it is omitted, DataBinder.Eval returns a value of type object, as in the following example:
<%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>
DataBinder.Eval is particularly useful when data-binding controls within a templated list, since often both the data row and the data field must be cast.
Example
The following 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.
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
// Rather than explictly pull out the variable from the StateList
// and then manipulate a label control, just call Page.DataBind.
// This will evaluate any <%# %> expressions within the page.
Page.DataBind();
}
</script>
</head>
<body>
<h3><font face="Verdana">Data 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 pull out the variable from the StateList
' and then manipulate 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">Data 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
Introduction to Web Forms Pages | ASP.NET Web Forms Syntax