溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用asp.net怎么在Repeater中使用復(fù)選框

發(fā)布時間:2021-06-08 16:20:13 來源:億速云 閱讀:198 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用asp.net怎么在Repeater中使用復(fù)選框,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

.aspx文件中:

<%--頂層Repeater--%>
        <asp:Repeater ID="rptChannel" runat="server">
            <itemtemplate>
             <br /><b><%# Eval("ChannelName")%></b>
             <%--嵌套的Repeater,指定使用后臺創(chuàng)建的Releation來獲取數(shù)據(jù)源--%>
              <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,指定使用后臺創(chuàng)建的Releation來獲取數(shù)據(jù)源--%>
            </itemtemplate>
        </asp:Repeater >
        <%--end 頂層Repeater--%>

.aspx.cs文件中:

#region Repeater嵌套的Repeater中使用復(fù)選框
      //★Repeater嵌套-經(jīng)典運用★
      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

//……略相關(guān)數(shù)據(jù)庫操作代碼

#region 設(shè)置Repeater嵌套的Repeater中相應(yīng)的復(fù)選框為選中狀態(tài)
          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中的復(fù)選框所選擇的值的組合,以","隔開
    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

關(guān)于使用asp.net怎么在Repeater中使用復(fù)選框就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI