.aspx文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%--頂層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文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#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嵌套的Repeater中使用復選框,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnsendblog.com/index.php/?p=109