gta4躲闪:asp.net 关于循环嵌套显示

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 04:52:17
想问下ASP.NET中REAPTER能不能嵌套显示表格内容,也就是说REAPTER里能不能再嵌套一个REAPTER,如果不行的话,那我想实现循环嵌套怎么办,谢谢

可以的Reapter里面可以再嵌套,但里面的那个就不能直接使用数据绑定了。 会提示找不到控件。 这时里面的控件的数据绑定应该放在外面控件的ItemDataBound事件里面来绑定。

实例:
private void MainBorad_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater sb = (Repeater) e.Item.FindControl("SubBorad");

DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取ID
int CatId = Convert.ToInt32(rowv["mID"]);
//根据分类ID查询该分类下的产品,并绑定子Repeater
sb.DataSource=......;
sb.DataBind();
}
}

REAPTER可以嵌套REAPTER。第一个REAPTER在代码中设置datasource,嵌套的REAPTER在页面设置datasource就可以实现嵌套了
举例:
后台:
private void Page_Load(object sender, System. EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection con=DB.createConnection();
if (con.State==ConnectionState.Closed)
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda1 = new SqlDataAdapter("select * from t_forum_board where boardlevel=1 and boardflag=1 order by boardid asc",con);
//Create and fill the DataSet.
sda1.Fill(ds,"forum_1");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter sda2 = new SqlDataAdapter("select * from t_forum_board where boardlevel=2 and boardflag=1 order by boardid asc",con);
sda2.Fill(ds,"forum_2");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",ds.Tables["forum_1"].Columns["BoardId"],ds.Tables["forum_2"].Columns["ParentId"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parent.DataSource = ds.Tables["forum_1"];
Page.DataBind();
//Close the connection.
con.Close();
}
页面上在进行嵌套的时候为第二个REAPTER设定DataSource
<asp:repeater id="child" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">

REAPTER可以嵌套REAPTER。第一个REAPTER在代码中设置datasource,嵌套的REAPTER在页面设置datasource就可以实现嵌套了
举例:
后台:
private void Page_Load(object sender, System. EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection con=DB.createConnection();
if (con.State==ConnectionState.Closed)
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda1 = new SqlDataAdapter("select * from t_forum_board where boardlevel=1 and boardflag=1 order by boardid asc",con);
//Create and fill the DataSet.
sda1.Fill(ds,"forum_1");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter sda2 = new SqlDataAdapter("select * from t_forum_board where boardlevel=2 and boardflag=1 order by boardid asc",con);
sda2.Fill(ds,"forum_2");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",ds.Tables["forum_1"].Columns["BoardId"],ds.Tables["forum_2"].Columns["ParentId"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parent.DataSource = ds.Tables["forum_1"];
Page.DataBind();
//Close the connection.
con.Close();
}
页面上在进行嵌套的时候为第二个REAPTER设定DataSource
<asp:repeater id="child" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">