您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。