Repeater控件的嵌套方法
2005-06-08 13:21 张剑 阅读(460) 评论(0) 编辑 收藏 举报1.WEB控件
<asp:repeater ID="Class" runat="server" OnItemCreated="Class_ItemCreated">
<itemtemplate>
<asp:repeater ID="Forum" runat="server">
<itemtemplate>
</itemtemplate>
</asp:repeater>
<itemtemplate>
</asp:repeater>
2.CS文件
//显示主分类
strSQL = "SELECT * FROM forum WHERE rootid = 0";
Adpt = new OleDbDataAdapter(strSQL, Conn);
Adpt.Fill(Ds, "Class");
Class.DataSource = Ds.Tables["Class"].DefaultView
//二级分类
protected DataTable Forum_Source(int id)
{
if (Ds.Tables.Contains("Forum") && Ds.Tables.CanRemove(Ds.Tables["Forum"]))
Ds.Tables.Remove("Forum");
strSQL = "SELECT * FROM forum WHERE rootid = @id";
Cmd = new OleDbCommand(strSQL, Conn);
Cmd.Parameters.Add(new OleDbParameter("@id", OleDbType.Integer)).Value = id;
Adpt = new OleDbDataAdapter(Cmd);
Adpt.Fill(Ds, "Forum");
return Ds.Tables["Forum"];
}
//控件调用的OnItemCreated类
protected void Class_ItemCreated(object sender, RepeaterItemEventArgs e)
{
((Repeater)e.Item.FindControl("Forum")).DataSource = Forum_Source(int.Parse(((System.Data.DataRowView)e.Item.DataItem)["id"].ToString())).DefaultView;
}