asp. net using check boxes in Repeater nested Repeater

  • 2021-10-27 07:07:54
  • OfStack

In the. aspx file:


<%-- Top floor Repeater--%>
        <asp:Repeater ID="rptChannel" runat="server">
            <itemtemplate>
             <br /><b><%# Eval("ChannelName")%></b>
             <%-- Nested Repeater, Specifies that the Releation To get the data source --%>
              <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  Nested Repeater, Specifies that the Releation To get the data source --%>
            </itemtemplate>
        </asp:Repeater >
        <%--end  Top floor Repeater--%>

In the. aspx. cs file:


#region Repeater Nested Repeater Use check boxes in 
      // ★ Repeater Nesting - Classic application  ★  
      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"];// Bind top layer Repeater (Note: As long as the top layer is bound, the nested layer cannot be bound.) 
      this.rptChannel.DataBind();
      #endregion

// ..... Slightly related database operation code 

#region  Settings Repeater Nested Repeater The corresponding check box in is selected 
          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  Get Repeater Nested Repeater The combination of values selected by the check box in the , With "," Separate 
    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 != "")
    {
      // Remove the last 1 Characters 
      //str_Teamflag = str_Teamflag.Substring(0, str_Teamflag.Length - 1);
      str_Teamflag = str_Teamflag.Remove(str_Teamflag.Length - 1);
    }
    #endregion

Related articles: