Repeater嵌套Repeater并取得嵌套Repeater里面的…

2018-06-17 20:52:47来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前台代码:

   <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 + ",";
                            }
                        }
                    }
                }
 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:总结一下工作中遇到的NPOI以及在ASP.NET MVC中的使用

下一篇:两个 DataTable 读取重复数据