这篇文章将为大家详细讲解有关使用asp.net怎么在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中使用复选框就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。