嵌套 DataList 通过DataSource属性进行梆定需要使用到onitemdatabound()事件
先看效果图吧:系统全局配置和权限管理等栏目存储在classid表,而子项目存储在item表中。梆定子项是依具classid表的ID。像这样的情况就可用嵌套DataList 和嵌套Repeater来梆定。DataList 与Repeater的区别在于。DataList 会生成table等标签而Repeater不会。
DataList 与Repeater大同小意,就用DataList 来举例吧
asp.aspx页面代码:
<form id="form1" runat="server">
<asp:CheckBoxList ID="cbxpop" runat="server" RepeatDirection="Horizontal"></asp:CheckBoxList>
<asp:DataList ID="ddl_first" runat="server" onitemdatabound="ddl_first_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="parentid" runat="server" Value='<%#Eval("id") %>' />
<input type="checkbox" class="maincbx" name="mainbox" value='<%#Eval("id") %>'/><b><%#Eval("name") %></b>
<asp:DataList ID="dll_pop" runat="server" CssClass="subs" RepeatDirection="Horizontal">
<ItemTemplate><input type="checkbox" name="subcbx" value='<%#Eval("id") %>'/><%#Eval("name")%> </ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnSubmit" runat="server" Text="保存数据"
onclick="btnSubmit_Click" />
</form>
.cs文件代码:
if (!IsPostBack) {
//绑定datalist
ddl_first.DataSource = oBarBll.GetList("parentid=0");
ddl_first.DataBind();
}
protected void ddl_first_ItemDataBound(object sender, DataListItemEventArgs e)
{
string cid = "-1";
foreach (Control ct in e.Item.Controls)
{
if (ct.GetType() == typeof(HiddenField))
{
cid = ((HiddenField)ct).Value;
}
if (ct.GetType() == typeof(DataList))
{
DataList ddl = (DataList)ct;
ddl.DataSource = oBarBll.GetList("parentid=" + cid + "");
ddl.DataBind();
}
}
}
大概也就这样吧。
转载于:https://www.cnblogs.com/sunnysmile/archive/2009/05/08/1452792.html
相关资源:asp.net实现DataList与Repeater嵌套绑定的方法