数据绑定语法 1. ASP.NET 声明性数据绑定语法使用 <%# %> 表示法。 2. 可以绑定到数据源、页或其他控件的属性、集合、表达式以及从方法调用返回的结果。 3. 语法示例 简单属性 Customer: <%# custID %> 集合 Orders: <asp:ListBox id="List1" datasource=''''<%# myArray %>'''' runat="server"> 表达式 Contact: <%# ( customer.First Name + " " + customer.LastName ) %> 方法结果 Outstanding Balance: <%# GetBalance(custID) %> 4. ASP Response.Write 与 DataBind 方法区别 ASP Response.Write 快捷方式语法在处理页时计算,而 ASP.NET 数据绑定语法仅在调用 DataBind 方法时计算。 5. DataBind 的级联 当在父控件上调用 DataBind 时,它级联到该控件的所有子控件。 例如,DataList1.DataBind() 将因此对 DataList 模板中的每一控件调用 DataBind 方法。在页上调用 DataBind — Page.DataBind() 或只是 DataBind() — 会导致计算页上的所有数据绑定表达式 6. DataBinder.Eval A.计算后期绑定的数据绑定表达式并且可选择将结果格式化为字符串。 B.<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %> C.DataBinder.Eval 会对标准数据绑定语法带来很明显的性能损失,因为它使用后期绑定反射,注意这一点很重要。使用 DataBinder.Eval 时需谨慎,尤其是在不需要字符串格式化时。 7.数据绑定表达式必须计算为 String 类型的值 自定义控件类的代码 using System; using System.Web; using System.Web.UI; namespace SimpleControlSamples { public class Simple : Control { protected override void Render(HtmlTextWriter output) { output.Write("<H2>欢迎使用控件开发!</H2>"); } } } aspx页引用该自定义控件的代码 <%@ Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" Assembly="SimpleControlSamples" %> <SimpleControlSamples:Simple id="MyControl" runat=server/> [1] [2] [3] 下一页 [C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|