使用DataList控件中嵌套GridView控件主要是为了解决数据分类绑定的问题。在做web应用程序开发时,特别是大型网站,后台处理数据时经常会遇到数据分类绑定。如两个互有关联的数据表同时按条件显示在页面。将GridView控件嵌套在DataList控件中,可以把分类信息显示在DataList控件中,在GridView控件中显示对应具体的信息。
在页面上创建下面的示例。在页面上添加一个DataList控件,并完成它的数据源配置,将SQL命令设置如下SELECT BianH,ZhuCYHM,XingM,XingB FROM T_YongH。进入到DataList控件的项模板编辑框中,在ItemTemplate模板编辑框中添加一个TextBox控件,将它绑定到ZhuCYHM字段上。接着在ItemTemplate模板编辑框添加GridView控件。这样就完成了前台代码的设置。
对应HTML代码如下所示:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
DataList_exam11.aspx.cs
"
Inherits
=
"
DataList_exam11
"
%>
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
DataList_exam11
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:datalist id
=
"
DataList1
"
Runat
=
"
server
"
ShowFooter
=
"
False
"
ShowHeader
=
"
False
"
OnItemDataBound
=
"
DataList1_ItemDataBound
"
>
<
ItemStyle VerticalAlign
=
"
Top
"
></
ItemStyle
>
<
ItemTemplate
>
<
TABLE id
=
"
Table1
"
cellSpacing
=
"
0
"
cellPadding
=
"
0
"
width
=
"
100%
"
border
=
"
0
"
style
=
"
height: 123px
"
>
<
TR bgColor
=
"
aliceblue
"
>
<
TD
class
=
"
title
"
style
=
"
height: 25px
"
>
用户名:
<
asp:HyperLink id
=
"
ZhuCYHM
"
runat
=
"
server
"
Font
-
Bold
=
"
True
"
Target
=
"
_blank
"
Text
=
'
<%# Eval("ZhuCYHM") %>
'
></
asp:HyperLink
><
br
/>
详细信息:
</
TD
>
</
TR
>
<
TR
>
<
TD vAlign
=
"
top
"
style
=
"
height: 133px
"
>
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
OnDataBinding
=
"
GridView1_DataBinding
"
>
<
Columns
>
<
asp:CommandField ShowEditButton
=
"
True
"
/>
</
Columns
>
</
asp:GridView
>
</
TD
>
</
TR
>
</
TABLE
>
</
ItemTemplate
>
</
asp:datalist
>&
nbsp;
</
div
>
</
form
>
</
body
>
</
html
>
接下来开始编写后台代码来实现具体嵌套功能。首先在Page_Load中实现DataList1控件的数据绑定,在此使用的是动态绑定数据,建立数据库连接,然后将SELECT BianH,ZhuCYHM,XingM,XingB FROM T_YongH查询出来的数据绑定到DataList1控件。最后使用DataList1_ItemDataBound事件实现在DataList控件中嵌套GridView控件的功能。具体代码如下所示:
public
partial
class
DataList_exam11 : System.Web.UI.Page
...
{ protected void Page_Load( object sender, EventArgs e) ... { // 声明一个字符串 string sConnectionString; // 连接数据库字符串,连接到XBMIS数据库,用户名是sa sConnectionString = " Data Source=.;Initial Catalog= XBMIS;User ID=sa; " ; // 创建SqlConnection数据库连接对象 SqlConnection Conn = new SqlConnection(sConnectionString); SqlDataAdapter Adapter = new SqlDataAdapter( " SELECT BianH, ZhuCYHM, XingM, XingB FROM T_YongH " , Conn); Conn.Open(); DataSet ds = new DataSet(); Adapter.Fill(ds, " yongh " ); DataList1.DataSource = ds.Tables[ " yongh " ].DefaultView; DataList1.DataBind(); } protected void DataList1_ItemDataBound( object sender, DataListItemEventArgs e) ... { // 只在itemType=Item和AlternatingItem进行处理 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) ... { // 取出此item数据 DataRowView dr = (DataRowView)e.Item.DataItem; // 找到此item里的GridView,这里是实现分类显示数据的关键 GridView GridView1 = (GridView)e.Item.FindControl( " GridView1 " ); // 根据此item的值查找ZhuCYHM和在记录模板中显示的注册用户名一样的数据记录,先要建立连接,然后填充数据 SqlConnection conn = new SqlConnection( " Data Source=.;Initial Catalog= XBMIS;User ID=sa; " ); SqlDataAdapter da = new SqlDataAdapter( " SELECT BianH, ZhuCYHM, XingM, XingB FROM T_YongH where ZhuCYHM=@ZhuCYHM " , conn); // 添加ZhuCYHM参数 da.SelectCommand.Parameters.Add(" @ZhuCYHM " , SqlDbType.NVarChar).Value = Convert.ToString(dr[ " ZhuCYHM " ]); // 给GridView添加DataBinding事件 DataSet ds = new DataSet(); da.Fill(ds, " yongh " ); // 绑定数据到GridView1 GridView1.DataBound += new EventHandler(GridView1_DataBinding); GridView1.DataSource = ds.Tables[ " yongh " ]; GridView1.DataBind(); } } protected void GridView1_DataBinding( object sender, EventArgs e) ... { // 必须生成GridView1_DataBinding事件 } }