当在页上调用
<tagprefix:tagname property="<%# data-binding expression %>" runat="server" /> - or - literal text <%# data-binding expression %> |
参数
- property
-
为其声明数据绑定的控件属性。
- data-binding expression
-
符合备注部分中概述的要求的任意表达式。
备注
所有数据绑定表达式都必须包含在 <%# 和 %> 字符之间。
ASP.NET 支持分层数据绑定模型,该模型创建服务器控件属性和数据源之间的绑定。几乎任何服务器控件属性都可以绑定到任何公共字段或属性,这些公共字段或属性位于包含页或服务器控件的直接命名容器上。
数据绑定表达式使用 Eval 和 Bind 方法将数据绑定到控件,并将更改提交回数据库。Eval 方法是静态(只读)方法,该方法采用数据字段的值作为参数并将其作为字符串返回。Bind 方法支持读/写功能,可以检索数据绑定控件的值并将任何更改提交回数据库。
可以使用 XPath 和 XPathSelect 方法以及
示例
下面的代码示例演示如何在 ASP.NET 服务器控件中根据属性进行数据绑定。当用户从
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> |
评论