打印本文 打印本文 关闭窗口 关闭窗口
《ASP.Net快速入门》学习笔记2
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2632  更新时间:2009/4/23 10:36:08  文章录入:mintao  责任编辑:mintao

数据绑定语法

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 方法。在上调用 DataBindPage.DataBind() 或只是 DataBind() — 会导致计算页上的所有数据绑定表达式

6.  DataBinder.Eval

A.计算后期绑定的数据绑定表达式并且可选择将结果格式化为字符串。

B.<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>

CDataBinder.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]  下一页

打印本文 打印本文 关闭窗口 关闭窗口