另外一种DataGrid与Datalist的嵌套--更好的方式[原创]

相对于上一次的使用Relation的缺点:
1. 不能限制返回的纪录数;2. 邦定时不方便,甚至不能邦定上;3. 程序简单,但不容易理解。4. 可能对于三层以上的嵌套不容易实现。
这次给出的嵌套方案,使用的ItemDataBound事件进行嵌套,实现起来很方便,其容易理解。并且解决了以上四个问题。
以下是主要的代码:

1<asp:datalist backcolor="#ffffff" borderwidth="0px" cellpadding="1" cellspacing="20" datakeyfield="NavigatorID" horizontalalign="Justify" id="DList_Class" repeatcolumns="2" repeatdirection="Horizontal" repeatlayout="Table" runat="server" showfooter="False" showheader="False">
2<itemstyle verticalalign="Top"></itemstyle>
3<itemtemplate>
4<b>```
5# DataBinder.Eval(Container.DataItem,"NavigatorID") 
6``` @   

DataBinder.Eval(Container.DataItem,"NodeCaption")

 1<hr/>
 2<asp:datagrid allowpaging="true" autogeneratecolumns="false" backcolor="#ffffff" bordercolor="#ffffff" borderwidth="1" cellpadding="0" cellspacing="0" gridlines="Horizontal" id="DG_SubClass" onitemdatabound="DG_SubClass_ItemDataBound" pagerstyle-visible="false" pagesize="10" runat="server" showfooter="false" showheader="false" width="315px">
 3<itemstyle backcolor="#e3f1ff" cssclass="gridHover" height="14px"></itemstyle>
 4<columns>
 5<asp:boundcolumn datafield="ModuleType" readonly="True">
 6<itemstyle horizontalalign="Center" width="25"></itemstyle>
 7</asp:boundcolumn>
 8<asp:boundcolumn datafield="NodeCaption">
 9<itemstyle cssclass="nav" horizontalalign="Left"></itemstyle>
10</asp:boundcolumn>
11<asp:boundcolumn datafield="ModuleType" readonly="True">
12<itemstyle horizontalalign="Center" width="90"></itemstyle>
13</asp:boundcolumn>
14</columns>
15</asp:datagrid>
16</itemtemplate>
17</asp:datalist>

private void DList_Class_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataGrid dg=(DataGrid)e.Item.FindControl("DG_SubClass");
string itemIndex =this.DList_Class.DataKeys[e.Item.ItemIndex].ToString();

string[] strSqlArray =new string[3]{"select Top 10 IOrder, ILevel, IParentID, NavigatorID, NodeCaption,ModuleID, ModuleType from NavigatorNodes where IParentID in ( ", itemIndex, ");"};
string strSql=string.Concat(strSqlArray);
string TableName ="SubClass"+itemIndex;

this.dbClass.AdapterFill(ds, CommandType.Text, strSql, TableName, new OleDbParameter[0]);

dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DG_SubClass_ItemDataBound);

dg.DataSource =ds.Tables[TableName].DefaultView;
dg.DataBind();
}
}

更多内容,欢迎访问ms.mblogger.cn/nono

Published At
Categories with Web编程
Tagged with
comments powered by Disqus