Share via


数据绑定表达式语法

[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]

当在页上调用 DataBind 方法时,数据绑定表达式创建服务器控件属性和数据源之间的绑定。 可以将数据绑定表达式包含在服务器控件开始标记中特性/值对的值一侧,或页中的任何位置。

<tagprefix:tagname property="<%# data-binding expression %>"
   runat="server" />
- or -
literal text <%# data-binding expression %>

参数

  • property
    为其声明数据绑定的控件属性。

  • 数据绑定表达式
    符合备注部分中概述的要求的任意表达式。

备注

所有数据绑定表达式都必须包含在 <%# 和 %> 字符之间。

ASP.NET 支持分层数据绑定模型,该模型创建服务器控件属性和数据源之间的绑定。 几乎任何服务器控件属性都可以绑定到任何公共字段或属性,这些公共字段或属性位于包含页或服务器控件的直接命名容器上。

数据绑定表达式使用 Eval 和 Bind 方法将数据绑定到控件,并将更改提交回数据库。 Eval 方法是静态(只读)方法,该方法采用数据字段的值作为参数并将其作为字符串返回。 Bind 方法支持读/写功能,可以检索数据绑定控件的值并将任何更改提交回数据库。

可以使用 XPath 和 XPathSelect 方法以及 XPathBinder 类从 XmlDataSource 控件绑定到 XML 数据。 有关更多信息,请参见 XmlDataSource Web Server Control Overview

示例

下面的代码示例演示如何在 ASP.NET 服务器控件中根据属性进行数据绑定。 当用户从 DropDownList Web 服务器控件选择某个状态时,Label Web 服务器控件将根据列表中的选定项进行绑定并显示选中的状态。

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

请参见

参考

XPathBinder

概念

Data-Binding Expressions Overview

Introduction to ASP.NET Web Pages

ASP.NET Web Page Syntax Overview

XmlDataSource Web Server Control Overview