asp.net在Repeater嵌套的Repeater中使用复选框详解
程序员文章站
2022-06-25 08:57:07
.aspx文件中:
<%--顶层repeater--%>
.aspx文件中:
<%--顶层repeater--%> <asp:repeater id="rptchannel" runat="server"> <itemtemplate> <br /><b><%# eval("channelname")%></b> <%--嵌套的repeater,指定使用后台创建的releation来获取数据源--%> <asp:repeater id="rptclassify" datasource='<%# eval("myrelation") %>' runat="server"> <itemtemplate> <input type="checkbox" id="chk_flagid" value='<%# eval("flagid")%>' runat="server" /> <asp:label id="lbl_flagname" runat="server" text='<%# eval("flagname")%>'></asp:label> </itemtemplate> </asp:repeater > <%--end 嵌套的repeater,指定使用后台创建的releation来获取数据源--%> </itemtemplate> </asp:repeater > <%--end 顶层repeater--%>
.aspx.cs文件中:
#region repeater嵌套的repeater中使用复选框 //★repeater嵌套-经典运用★ string sqlstr1, sqlstr2; sqlstr1 = "select distinct a.channelid,b.channelname from ie_flaggroup a left join ie_channel b on a.channelid=b.channelid where a.isclose=0 order by a.channelid asc"; sqlstr2 = "select * from ie_flaggroup where isclose=0 order by flagid asc"; dataset dschannel = dbfun.datasettwo(sqlstr1, "channel", sqlstr2, "classify", "myrelation"); dschannel.relations.add("myrelation", dschannel.tables["channel"].columns["channelid"], dschannel.tables["classify"].columns["channelid"], false); this.rptchannel.datasource = dschannel.tables["channel"];//绑定顶层repeater(注意:只要绑定顶层就好,嵌套层不能绑定) this.rptchannel.databind(); #endregion //……略相关数据库操作代码 #region 设置repeater嵌套的repeater中相应的复选框为选中状态 string[] selteamflag = drw["teamflag"].tostring().split(','); htmlinputcheckbox checkbox; repeater rpclass; for (int i = 0; i < this.rptchannel.items.count; i++) { rpclass = (repeater)this.rptchannel.items[i].findcontrol("rptclassify"); for (int j = 0; j < rpclass.items.count; j++) { checkbox = (htmlinputcheckbox)rpclass.items[j].findcontrol("chk_flagid"); if (selteamflag.contains(checkbox.value)) checkbox.checked = true; } } #endregion #region 获取repeater嵌套的repeater中的复选框所选择的值的组合,以","隔开 string str_teamflag = ""; htmlinputcheckbox checkbox; repeater rpclass; for (int i = 0; i < this.rptchannel.items.count; i++) { rpclass = (repeater)this.rptchannel.items[i].findcontrol("rptclassify"); for (int j = 0; j < rpclass.items.count; j++) { checkbox = (htmlinputcheckbox)rpclass.items[j].findcontrol("chk_flagid"); if (checkbox.checked) str_teamflag += checkbox.value + ","; } } if (str_teamflag != "") { //去除最后一个字符 //str_teamflag = str_teamflag.substring(0, str_teamflag.length - 1); str_teamflag = str_teamflag.remove(str_teamflag.length - 1); } #endregion
以上所述是小编给大家介绍的asp.net在repeater嵌套的repeater中使用复选框,希望对大家有所帮助