前台代码:
<asp:Repeater ID="RepeaterScene" runat="server" OnItemDataBound="RepeaterList_ItemDataBound">
<ItemTemplate>
<tr>
<td class="bg_1" style="text-align: right; width: 75px; height: 36px;">
<%#Eval("ClassName") %>:
</td>
<asp:HiddenField ID="hideClassId" runat="server" Value='<%#Eval("ClassId") %>' />
<asp:Repeater ID="RepeaterClassScene" runat="server">
<ItemTemplate>
<td>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
</asp:CheckBoxList>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
你要弄清楚,当绑定Repeater的数据源中没有值时,上面这些东西不显示在界面中,这样就可以做到添加数据后在前台动态显示。
有数据时,显示效果
后台代码:
//显示所有景色类型
PRivate void BindScene()
{
DataTable scene = new NJL.Bll.SceneTypeClass().GetScene(" where ParentId=0 and isshow=1");
RepeaterScene.DataSource = scene;
RepeaterScene.DataBind();
}
// Repeater 的ItemDataBound 方法
protected void RepeaterList_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
HiddenField hideClassId = (HiddenField)e.Item.FindControl("hideClassId");
if(!string.IsNullOrEmpty(hideClassId.Value))
{
Repeater relist = (Repeater)e.Item.FindControl("RepeaterClassScene");
string where =string.Format(" where isshow=1 and ParentId='{0}'",hideClassId.Value);
DataTable cla = new NJL.Bll.SceneTypeClass().GetScene(where);
relist.DataSource = cla;
relist.DataBind();
CheckBoxList ck = (CheckBoxList)relist.Items[0].FindControl("CheckBoxList1");
ck.DataValueField = "ClassId";
ck.DataTextField = "ClassName";
ck.DataSource = cla;
ck.DataBind();
}
}
}
//获取设置选中的类型 获取Repeater内嵌的控件CheckBoxList 需要遍历
if(!string.IsNullOrEmpty(m.SceneType))
{
string[] follow = m.SceneType.Split(',');
for (int i = 0; i < RepeaterScene.Items.Count;i++ )
{
Repeater rep2 = (Repeater)RepeaterScene.Items[i].FindControl("RepeaterClassScene");
for (int j = 0; j < rep2.Items.Count;j++ )
{
CheckBoxList cbx = (CheckBoxList)rep2.Items[j].FindControl("CheckBoxList1");
foreach(ListItem item in cbx.Items)
{
foreach(string s in follow)
{
if(item.Value==s)
{
item.Selected = true;
}
}
}
}
}
}
//保存选中的景点类型
//旅游景点类型
string scenlist="";
for (int s = 0; s < RepeaterScene.Items.Count;s++ )
{
Repeater rep2 = (Repeater)RepeaterScene.Items[s].FindControl("RepeaterClassScene");
for (int x = 0; x < rep2.Items.Count;x++ )
{
CheckBoxList cbx = (CheckBoxList)rep2.Items[x].FindControl("CheckBoxList1");
foreach(ListItem item in cbx.Items)
{
if(item.Selected==true)
{
scenlist += item.Value + ",";
}
}
}
}