当前位置:Gxlcms > asp.net > asp.net在Repeater嵌套的Repeater中使用复选框详解

asp.net在Repeater嵌套的Repeater中使用复选框详解

时间:2021-07-01 10:21:17 帮助过:39人阅读

.aspx文件中:

  1. <%--顶层Repeater--%>
  2. <asp:Repeater ID="rptChannel" runat="server">
  3. <itemtemplate>
  4. <br /><b><%# Eval("ChannelName")%></b>
  5. <%--嵌套的Repeater,指定使用后台创建的Releation来获取数据源--%>
  6. <asp:Repeater ID="rptClassify" DataSource='<%# Eval("myrelation") %>' runat="server">
  7. <itemtemplate>
  8. <input type="checkbox" id="chk_FlagID" value='<%# Eval("FlagID")%>' runat="server" />
  9. <asp:Label ID="lbl_FlagName" runat="server" Text='<%# Eval("FlagName")%>'></asp:Label>
  10. </itemtemplate>
  11. </asp:Repeater >
  12. <%--end 嵌套的Repeater,指定使用后台创建的Releation来获取数据源--%>
  13. </itemtemplate>
  14. </asp:Repeater >
  15. <%--end 顶层Repeater--%>

.aspx.cs文件中:

  1. #region Repeater嵌套的Repeater中使用复选框
  2. //★Repeater嵌套-经典运用★
  3. string sqlstr1, sqlstr2;
  4. 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";
  5. sqlstr2 = "select * from IE_FlagGroup where isClose=0 order by FlagID asc";
  6. DataSet dsChannel = DBFun.dataSetTwo(sqlstr1, "Channel", sqlstr2, "Classify", "myrelation");
  7. dsChannel.Relations.Add("myrelation", dsChannel.Tables["Channel"].Columns["ChannelID"], dsChannel.Tables["Classify"].Columns["ChannelID"], false);
  8. this.rptChannel.DataSource = dsChannel.Tables["Channel"];//绑定顶层Repeater(注意:只要绑定顶层就好,嵌套层不能绑定)
  9. this.rptChannel.DataBind();
  10. #endregion
  11. //……略相关数据库操作代码
  12. #region 设置Repeater嵌套的Repeater中相应的复选框为选中状态
  13. string[] selTeamflag = drw["Teamflag"].ToString().Split(',');
  14. HtmlInputCheckBox checkBox;
  15. Repeater rpClass;
  16. for (int i = 0; i < this.rptChannel.Items.Count; i++)
  17. {
  18. rpClass = (Repeater)this.rptChannel.Items[i].FindControl("rptClassify");
  19. for (int j = 0; j < rpClass.Items.Count; j++)
  20. {
  21. checkBox = (HtmlInputCheckBox)rpClass.Items[j].FindControl("chk_FlagID");
  22. if (selTeamflag.Contains(checkBox.Value))
  23. checkBox.Checked = true;
  24. }
  25. }
  26. #endregion
  27. #region 获取Repeater嵌套的Repeater中的复选框所选择的值的组合,以","隔开
  28. string str_Teamflag = "";
  29. HtmlInputCheckBox checkBox;
  30. Repeater rpClass;
  31. for (int i = 0; i < this.rptChannel.Items.Count; i++)
  32. {
  33. rpClass = (Repeater)this.rptChannel.Items[i].FindControl("rptClassify");
  34. for (int j = 0; j < rpClass.Items.Count; j++)
  35. {
  36. checkBox = (HtmlInputCheckBox)rpClass.Items[j].FindControl("chk_FlagID");
  37. if (checkBox.Checked)
  38. str_Teamflag += checkBox.Value + ",";
  39. }
  40. }
  41. if (str_Teamflag != "")
  42. {
  43. //去除最后一个字符
  44. //str_Teamflag = str_Teamflag.Substring(0, str_Teamflag.Length - 1);
  45. str_Teamflag = str_Teamflag.Remove(str_Teamflag.Length - 1);
  46. }
  47. #endregion

以上所述是小编给大家介绍的asp.net在Repeater嵌套的Repeater中使用复选框,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行