正文

数据绑定表达式语法 2007-01-31 11:15:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/22996.html

分享到:

当在页上调用 DataBind 方法时,数据绑定表达式创建服务器控件属性和数据源之间的绑定。可以将数据绑定表达式包含在服务器控件开始标记中属性/值对的值一侧,或页中的任何位置。 <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 方法以及 XPathBinder 类从 XmlDataSource 控件绑定到 XML 数据。有关更多信息,请参见 XmlDataSource Web 服务器控件。 示例 下面的代码示例演示如何在 ASP.NET 服务器控件中根据属性进行数据绑定。当用户从 DropDownList Web 服务器控件选择某个状态时,Label Web 服务器控件将根据列表中的选定项进行绑定并显示选中的状态。 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>

阅读(3352) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册