此处需要下面的命名空间:

  • System.Data
  • System.Data.SqlClient

使用ASP.NET数据绑定,可以将任何服务器控件绑定到简单的属性、集合、表达式、方法。

此篇的主要内容:

  • 概要
  1. <%#…%>语法
  2. Page.DataBind与Control.DataBind
  • 数据绑定列表控件
  • Repeater
  • DataList
  • DataGrid
  • 访问数据
  • DataSet类
  • DataReader类
  • 列表控件模板中的绑定
  • DataBinder.Eval方法
  • 显示转换
  • ItemDataBound事件

<%#…%>语法

它是 在Web页面中 使用数据绑定的基础。

所有数据绑定表达式必须包含在这些字符中

 

可以从多个源进行简单数据绑定

  • 简单属性,例如
<%# custId %>


  • 集合
<asp:ListBox id="List1" datasource="<%# myArray %>" runat="server"/>

 

  • 表达式
<%# (customer.FirstName + “ “ + customer.LastName) %>

 

  • 方法结果
<%# GetBalance(custId) %>


<%#…%>内联标记 用于 指示 将要把特定数据源中的信息

 

Page.DataBind与Control.DataBind

在为web页上的“对象”确定并设置 特定数据源 后,必须将数据绑定到这些数据源

这两种方法的使用方法很相似。主要差别在于:

调用Page.DataBind方法之后,所有数据源都将绑定到他们的服务器控件。通常,可以从Page_Load事件调用Page.DataBind(或DataBind)。

注意:在显示调用Web服务器控件的DataBind方法 或 在调用页面级的Page.DataBind方法之前不会有任何数据呈现给控件

 


 

数据绑定列表 控件

 

列表控件是可以绑定到集合的Web服务器控件。可以使用这些控件以自定义的模板格式显示数据行。

所有的列表控件 都有DataSource、DataMember公开属性,用于绑定到集合。

这些控件可以将其DataSource属性绑定到 实现了IEnumerable、ICollection、IListSource接口的任一集合。

 

Repeater控件

它是 模板化 的数据绑定列表。

Repeater是“无外观的”(即,它不具有任何内置布局或样式)因此,必须在控件的模板中明确声明所有HTML布局标记、格式标记和样式标记

 

列表控件模板中的绑定

可以使用列表控件中的模板来 绑定 和 自定义数据源 的各个记录。

 

下面介绍三种可用于 执行此操作的方法

DataBinder.Eval方法

当 数据源 处理 从数据库返回的数据 时,它 可能包含 很多份 信息。

可以 使用通用的 DataBinder.Eval方法 返回数据

<%# DataBinder.Eval(Container.DataItem,"au_id") %> <%—- “au_id”字段是从

容器对象的数据源中返回的

--%>

显示转换

如果需要更多控件,可以使用显示转换

显示转换 使用 类型转换关键字。(这些 关键字 充当 函数,由 编译器 生成 内联代码)

<%@ Page Language="VB" %>

<%@ Import Namespace="system.data" %>
<%@ Import Namespace="system.data.sqlclient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim conn As New SqlConnection("data source=.;initial catalog=pubs;user=sa;pwd=123")
Dim da As New SqlDataAdapter("select * from authors", conn)
Dim ds As New DataSet
        da.Fill(ds)
Dim ds2 As New DataSet
        da.Fill(ds2)
Dim ds3 As New DataSet
        da.Fill(ds3)
'
        r1.DataSource = ds
        r2.DataSource = ds2
        r3.DataSource = ds3
'
        Page.DataBind()
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="r1" runat="server">
    <%--使用DataTable做数据源 --%>
    <ItemTemplate> <%#CType(Container.DataItem, DataRowView)("au_id")%> <br /></ItemTemplate>
    </asp:Repeater>
    <hr />
    <asp:Repeater ID="r2" runat="server">
    <%-- 使用DataReader做数据源  --%>
  <%--  <ItemTemplate><%#CType(Container.DataItem, System.Data.Common.DbDataRecord)("au_lname")%></ItemTemplate>
    --%>
    </asp:Repeater>
    <asp:Repeater ID="r3" runat="server">
    <ItemTemplate>
        <%#CType(Container.DataItem, DataRowView)(0)%><br />
    </ItemTemplate>
    </asp:Repeater>
    </div>
    </form>
</body>
</html>

 

ItemDataBound事件

可以使用控件的ItemDataBound事件绑定数据

将某个项目的数据 绑定到 控件 时,就会发生此事件。

<%@ Page Language="VB" %>
<%-- 加入命名空间 --%>
<%@ Import Namespace=system.data %>
<%@ Import Namespace=system.data.sqlclient %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'取得 数据 并 绑定到控件上
        Dim conn As New SqlConnection("data source=.;initial catalog=pubs;user=sa;pwd=123")
Dim da As New SqlDataAdapter("select * from authors", conn)
Dim ds As New DataSet
        da.Fill(ds)
        rptr.DataSource = ds
        rptr.DataBind()
End Sub

Protected Sub rptr_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
'数据保存在 e.Item.DataItem中,类型是DataRowView
        Dim rec As DataRowView
        rec = e.Item.DataItem
'取出数据,放在绑定控件中
        If Not IsDBNull(rec) Then
Dim l1 As Label
            l1 = e.Item.FindControl("lblAuthorId")
            l1.Text = rec("au_id").ToString
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%--第一步先加入控件--%>
    <asp:Repeater ID=rptr runat=server OnItemDataBound="rptr_ItemDataBound">
    <ItemTemplate>
        <asp:Label ID=lblAuthorId runat=server></asp:Label><br />
    </ItemTemplate>
    </asp:Repeater>
    </div>
    </form>
</body>
</html>