说明:简单的说一下.net2.0中GridView的无限级嵌套(稍加修改就是Repeater..的无限级嵌套),当然您在实际的开发中并不一定用得到或者能被其他的方法(自定义控件等)代替.但是前几天在做一个东西的时候涉及到要显示:大类包含不定数个小类,而且小类又包含不定数个小小类.简单的说就是:一对多,多对多,如图:
下面我就以三层嵌套说明一下!(无限级可在此基础上比葫芦画瓢)
首先您要是对数据加载和事件的触发以及他们的顺序不太清楚的,请参考:
http://overred.cnblogs.com/archive/2006/02/23/336462.html
++++++++++.aspx代码++++++++++++++++
OnRowDataBound事件,与1.x中的ItemDataBound有一腿.
<%#Container.DataItem.ToString()%>
<%#Container.DataItem.ToString()%>
<%#Container.DataItem.ToString()%>
++++++++++.cs代码++++++++++++++++
RowDataBound与1.x中的ItemDataBound也有一腿!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Al("GridView1Item:");
this.GridView1.DataBind();
}
//DataSource
protected ArrayList Al(string GetStr)
{
ArrayList al = new ArrayList();
for(int i=0;i<2;i++)
{
al.Add(GetStr+i.ToString());
}
return al;
}
//GridView1_RowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView2");
gv.DataSource = Al("GridView2Item:");
gv.DataBind();
}
}
//GridView2_RowDataBound
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView3");
gv.DataSource = Al("GridView3Item:");
gv.DataBind();
}
}
}
本文作者:佚名 来源:http://www.cnblogs.com/overred/
CIO之家 www.ciozj.com 微信公众号:imciow