本文出自:Web程序设计与架构(蔺华 王玉清 电子工业出版社)
1.4 数据绑定基础
1.4.1 数据绑定概述和语法
ASP.NET引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。表1-2显示了新语法的一些示例。
表1-2 ASP.NET引入的新的声明性数据绑定语法
简单属性 | Customer: <%# custID %> |
集合 | Orders: <asp:ListBox id="List1" datasource= '<%# myArray %>' runat="server"> |
表达式 | Contact: <%# ( customer.First Name + " " + customer.LastName ) %> |
方法结果 | Outstanding Balance: <%# GetBalance(custID) %> |
尽管该语法看起来与ASP的Response.Write快捷方式<%= %>相似,但其行为完全不同。ASP Response.Write快捷方式语法在处理页时进行计算,而ASP.NET数据绑定语法仅在调用DataBind方法时计算。
DataBind是页和所有服务器控件的方法。当在父控件上调用DataBind时,它级联到该控件的所有子控件。例如,DataList1.DataBind() 将因此对DataList模板中的每一控件调用DataBind方法。在页上调用DataBind - Page.DataBind() 或只是DataBind() 会导致计算页上的所有数据绑定表达式。通常从Page_Load事件调用DataBind,如下例所示:
protected void Page_Load(Object Src, EventArgs E) { DataBind(); }
如果绑定表达式在运行时计算为预期的数据类型,则可以在.aspx页声明节中的几乎任何位置使用绑定表达式。上面的简单属性、表达式和方法示例在计算时向用户显示文本。在这些情况下,数据绑定表达式必须计算为String类型的值。在集合示例中,数据绑定表达式计算ListBox的DataSource属性的有效类型值。你会发现有必要转换绑定表达式中的类型值,以产生所需的结果。例如,如果count是整数:
Number of Records: <%# count.ToString() %>
1.4.2 绑定到简单属性
ASP.NET数据绑定语法支持绑定到公共变量、页的属性和页上其他控件的属性。
下面的示例说明如何绑定到公共变量和页上的简单属性。注意这些值在DataBind() 调用前要初始化。
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object sender, EventArgs e) {
- Page.DataBind();
- }
- string custID{
- get {
- return "ALFKI";
- }
- }
- int orderCount{
- get {
- return 11;
- }
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">到页属性的数据绑定</font></h3>
- <form runat=server>
- 客户:<b><%# custID %></b><br>
- 未结的订单:<b><%# orderCount %></b>
- </form>
- </body>
- </html>
下面的示例说明如何绑定到另一控件的属性。
- <html>
- <head>
- <script language="C#" runat="server">
- void SubmitBtn_Click(Object sender, EventArgs e) {
- // 仅调用"Page.DataBind",而不是从"StateList"
- // 中显式取出变量,然后操作标签控件。
- // 这将计算页内所有的 <%# %> 表达式
- Page.DataBind();
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">到另一个服务器控件的属性的数据绑定</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="提交" OnClick="SubmitBtn_Click" runat=server/>
- <p>
- 选定的州:<asp:label text='<%# StateList.SelectedItem.Text %>' runat=server/>
- </form>
- </body>
- </html>
1.4.3 绑定到集合和列表
像DataGrid、ListBox和HTMLSelect这样的列表服务器控件将集合用作数据源。下面的示例说明如何绑定到通常的公共语言运行库集合类型。这些控件只能绑定到支持IEnumerable、ICollection或IListSource接口的集合。最常见的是绑定到ArrayList、Hashtable、DataView和DataReader。
绑定到ArrayList的示例如下。
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object Sender, EventArgs E) {
- if (!Page.IsPostBack) {
- ArrayList values = new ArrayList();
- values.Add ("IN");
- values.Add ("KS");
- values.Add ("MD");
- values.Add ("MI");
- values.Add ("OR");
- values.Add ("TN");
- DropDown1.DataSource = values;
- DropDown1.DataBind();
- }
- }
- void SubmitBtn_Click(Object sender, EventArgs e) {
- Label1.Text = "您选择了:" + DropDown1.SelectedItem.Text;
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">数据绑定 DropDownList</font></h3>
- <form runat=server>
- <asp:DropDownList id="DropDown1" runat="server" />
- <asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
- <p>
- <asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat= "server" />
- </form>
- </body>
- </html>
下面的示例说明如何绑定到DataView。注意DataView类在System.Data命名空间中定义。
- <%@ Import namespace="System.Data" %>
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object sender, EventArgs e ) {
- if (!Page.IsPostBack) {
- DataTable dt = new DataTable();
- DataRow dr;
- dt.Columns.Add(new DataColumn("整数值", typeof(Int32)));
- dt.Columns.Add(new DataColumn("字符串值", typeof(string)));
- dt.Columns.Add(new DataColumn("日期时间值", typeof(Date Time)));
- dt.Columns.Add(new DataColumn("布尔值", typeof(bool)));
- for (int i = 1; i <= 9; i++) {
- dr = dt.NewRow();
- dr[0] = i;
- dr[1] = "项 " + i.ToString();
- dr[2] = DateTime.Now;
- dr[3] = (i % 2 != 0) ? true : false;
- dt.Rows.Add(dr);
- }
- dataGrid1.DataSource = new DataView(dt);
- dataGrid1.DataBind();
- }
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">到 DataView 的数据绑定</font></h3>
- <form runat=server>
- <asp:DataGrid id="dataGrid1" runat="server"
- BorderColor="black"
- BorderWidth="1"
- GridLines="Both"
- CellPadding="3"
- CellSpacing="0"
- HeaderStyle-BackColor="#aaaadd"
- />
- </form>
- </body>
- </html>
绑定到Hashtable的示例如下。
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object sender, EventArgs e) {
- if (!Page.IsPostBack) {
- Hashtable h = new Hashtable();
- h.Add ("键 1", "值 1");
- h.Add ("键 2", "值 2");
- h.Add ("键 3", "值 3");
- MyDataList.DataSource = h;
- MyDataList.DataBind();
- }
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">到哈希表的数据绑定</font></h3>
- <form runat=server>
- <asp:DataList id="MyDataList" runat="server"
- BorderColor="black"
- BorderWidth="1"
- GridLines="Both"
- CellPadding="4"
- CellSpacing="0"
- >
- <ItemTemplate>
- <%# ((DictionaryEntry)Container.DataItem).Key %> :
- <%# ((DictionaryEntry)Container.DataItem).Value %>
- </ItemTemplate>
- </asp:DataList>
- </form>
- </body>
- </html>
1.4.4 绑定到表达式或方法
通常需要在绑定到页或控件之前操作数据。绑定到表达式或方法的返回值的示例如下。
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object Src, EventArgs E) {
- if (!Page.IsPostBack) {
- ArrayList values = new ArrayList();
- values.Add (0);
- values.Add (1);
- values.Add (2);
- values.Add (3);
- values.Add (4);
- values.Add (5);
- values.Add (6);
- DataList1.DataSource = values;
- DataList1.DataBind();
- }
- }
- String EvenOrOdd(int number) {
- if ((number % 2) == 0)
- return "偶数";
- else
- return "奇数";
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">到方法和表达式的数据绑定</font></h3>
- <form runat=server>
- <asp:DataList id="DataList1" runat="server"
- BorderColor="black"
- BorderWidth="1"
- GridLines="Both"
- CellPadding="3"
- CellSpacing="0"
- >
- <ItemTemplate>
- 数字值:<%# Container.DataItem %>
- 偶/奇:<%# EvenOrOdd((int) Container.DataItem) %>
- </ItemTemplate>
- </asp:datalist>
- </form>
- </body>
- </html>
1.4.5 DataBinder.Eval()
ASP.NET框架提供了一种静态方法,计算后期绑定的数据绑定表达式,并且可选择将结果格式化为字符串。DataBinder.Eval很方便,因为它消除了开发人员为强迫将值转换为所需的数据类型而必须做的许多显式转换。这在数据绑定模板列表内的控件时尤其有用,因为通常数据行和数据字段的类型都必须转换。
请看下面的示例,本例中整数将显示为货币字符串。使用标准的ASP.NET数据绑定语法,必须首先转换数据行的类型,以便检索数据字段IntegerValue。下一步,将此作为参数传递给String.Format方法。
- <%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>
该语法比较复杂,难以记忆。相反,DataBinder.Eval只是一个具有三个参数的方法:数据项的命名容器、数据字段名和格式字符串。在像DataList、DataGrid或Repeater这样的模板列表中,命名容器始终是Container.DataItem。Page是另一个可与DataBinder.Eval一起使用的命名容器。
- <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式字符串参数是可选的。如果省略它,则DataBinder.Eval返回对象类型的值,如以下示例所示。
- <%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>
DataBinder.Eval会对标准数据绑定语法带来很明显的性能损失,因为它使用后期绑定反射,注意这一点很重要。使用DataBinder.Eval时需要谨慎,尤其是在不需要字符串格式化时。
- <%@ Import namespace="System.Data" %>
- <html>
- <head>
- <script language="C#" runat="server">
- void Page_Load(Object sender, EventArgs e) {
- if (!Page.IsPostBack) {
- DataTable dt = new DataTable();
- DataRow dr;
- dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
- dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
- dt.Columns.Add(new DataColumn("DateTimeValue", typeof(Date Time)));
- dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
- for (int i = 0; i < 9; i++) {
- dr = dt.NewRow();
- dr[0] = i;
- dr[1] = "项 " + i.ToString();
- dr[2] = DateTime.Now;
- dr[3] = (i % 2 != 0) ? true : false;
- dt.Rows.Add(dr);
- }
- dataList1.DataSource = new DataView(dt);
- dataList1.DataBind();
- }
- }
- </script>
- </head>
- <body>
- <h3><font face="宋体">使用 DataBinder.Eval 进行数据绑定</font></h3>
- <form runat=server>
- <asp:DataList id="dataList1" runat="server"
- RepeatColumns="3"
- Width="80%"
- BorderColor="black"
- BorderWidth="1"
- GridLines="Both"
- CellPadding="4"
- CellSpacing="0"
- >
- <ItemTemplate>
- 订购日期:<%# DataBinder.Eval(Container.DataItem, "DateTime Value", "{0:d}") %>
- <p>
- 数量:<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N2}") %>
- <p>
- 项:<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
- 订购日期: <asp:CheckBox id=chk1 Checked='<%# (bool)DataBinder. Eval (Container.DataItem, "BoolValue") %>' runat=server/>
- <p>
- </ItemTemplate>
- </asp:Datalist>
- </form>
- </body>
- </html>