Repeater和CheckBox控件(客户端和服务器端)实行全选或多条选择
客服端代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterCheckBox1.aspx.cs" Inherits="CheckBoxes.RepeaterCheckBox1" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" >
6 <head runat="server">
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8 <title>Repeater和CheckBox控件(客户端和服务端)实行全选和多条选择</title>
9 <style type="text/css">
10 .HiddenText label {display:none;}
11 </style>
12
13 </head>
14 <body>
15 <form id="form1" runat="server">
16 <div><table>
17 <asp:Repeater runat="server" ID="Repeater1">
18 <ItemTemplate>
19 <tr>
20 <td>
21 <%--服务器端 <asp:CheckBox ID="CheckBox2" runat="server" Text= CssClass="HiddenText" AutoPostBack="true" />--%>
22 <input type='checkbox' id='ChkSelect' class='nogrid' runat="server" value='<%#Eval("ItemID")%>'/>
23
24
25 </td>
26 </tr>
27
28 </ItemTemplate>
29 </asp:Repeater>
30 </table>
31 <input id="Checkbox1" type="checkbox" onclick='selectAll()' />
32 <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
33 </div>
34 </form>
35 </body>
36 </html>
37 <script language="javascript" type="text/javascript" >
38 ///选中所有的CheckBox
39 function selectAll()
40 {
41 // 获得用户页面中的所有的 输入功能的控件getElementById("ChkSelect").
42 var checkbox = document.getElementsByTagName("input");
43 if(checkbox[0].checked == true)
44 {
45 for (var i=0; i<checkbox.length; i++)
46 checkbox[i].checked = false;
47 }
48 else
49 {
50 for (var i=0; i<checkbox.length; i++)
51 checkbox[i].checked = true;
52 }
53 }
54 </script>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" >
6 <head runat="server">
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8 <title>Repeater和CheckBox控件(客户端和服务端)实行全选和多条选择</title>
9 <style type="text/css">
10 .HiddenText label {display:none;}
11 </style>
12
13 </head>
14 <body>
15 <form id="form1" runat="server">
16 <div><table>
17 <asp:Repeater runat="server" ID="Repeater1">
18 <ItemTemplate>
19 <tr>
20 <td>
21 <%--服务器端 <asp:CheckBox ID="CheckBox2" runat="server" Text= CssClass="HiddenText" AutoPostBack="true" />--%>
22 <input type='checkbox' id='ChkSelect' class='nogrid' runat="server" value='<%#Eval("ItemID")%>'/>
23
24
25 </td>
26 </tr>
27
28 </ItemTemplate>
29 </asp:Repeater>
30 </table>
31 <input id="Checkbox1" type="checkbox" onclick='selectAll()' />
32 <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
33 </div>
34 </form>
35 </body>
36 </html>
37 <script language="javascript" type="text/javascript" >
38 ///选中所有的CheckBox
39 function selectAll()
40 {
41 // 获得用户页面中的所有的 输入功能的控件getElementById("ChkSelect").
42 var checkbox = document.getElementsByTagName("input");
43 if(checkbox[0].checked == true)
44 {
45 for (var i=0; i<checkbox.length; i++)
46 checkbox[i].checked = false;
47 }
48 else
49 {
50 for (var i=0; i<checkbox.length; i++)
51 checkbox[i].checked = true;
52 }
53 }
54 </script>
服务器端代码
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Collections.Generic;
12
13 namespace CheckBoxes
14 {
15 public partial class RepeaterCheckBox1 : System.Web.UI.Page
16 {
17 /// <summary>
18 /// 加载
19 /// 涂聚文
20 /// </summary>
21 /// <param name="sender"></param>
22 /// <param name="e"></param>
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 //第一次加载页
26 if (!Page.IsPostBack)
27 {
28 DataTable dt = new DataTable();
29 dt.Columns.Add(new DataColumn("ItemID", System.Type.GetType("System.Int32")));
30 dt.Columns.Add(new DataColumn("ItemDescription", System.Type.GetType("System.String")));
31 dt.Columns.Add(new DataColumn("Flag", System.Type.GetType("System.Boolean")));
32
33 //Add some data
34 dt.Rows.Add(1, "apple", false);
35 dt.Rows.Add(2, "carrot", true);
36 dt.Rows.Add(3, "peach", false);
37 Repeater1.DataSource = dt;
38 Repeater1.DataBind();
39 }
40 }
41 /// <summary>
42 /// 选择
43 /// </summary>
44 /// <param name="sender"></param>
45 /// <param name="e"></param>
46 protected void Button1_Click(object sender, EventArgs e)
47 {
48 string s = "";
49 for (int i = 0; i < this.Repeater1.Items.Count; i++)
50 {
51 //客户端
52 HtmlInputCheckBox chb = (HtmlInputCheckBox)this.Repeater1.Items[i].FindControl("ChkSelect");
53 //CheckBox chb = (CheckBox)this.Repeater1.Items[i].FindControl("CheckBox2"); //服务器端
54 if (chb.Checked == true)
55 {
56 s = s + chb.Value;//chb.Text //服务器端
57 }
58 }
59 Response.Write(s);
60 }
61 }
62 }
63
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Collections.Generic;
12
13 namespace CheckBoxes
14 {
15 public partial class RepeaterCheckBox1 : System.Web.UI.Page
16 {
17 /// <summary>
18 /// 加载
19 /// 涂聚文
20 /// </summary>
21 /// <param name="sender"></param>
22 /// <param name="e"></param>
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 //第一次加载页
26 if (!Page.IsPostBack)
27 {
28 DataTable dt = new DataTable();
29 dt.Columns.Add(new DataColumn("ItemID", System.Type.GetType("System.Int32")));
30 dt.Columns.Add(new DataColumn("ItemDescription", System.Type.GetType("System.String")));
31 dt.Columns.Add(new DataColumn("Flag", System.Type.GetType("System.Boolean")));
32
33 //Add some data
34 dt.Rows.Add(1, "apple", false);
35 dt.Rows.Add(2, "carrot", true);
36 dt.Rows.Add(3, "peach", false);
37 Repeater1.DataSource = dt;
38 Repeater1.DataBind();
39 }
40 }
41 /// <summary>
42 /// 选择
43 /// </summary>
44 /// <param name="sender"></param>
45 /// <param name="e"></param>
46 protected void Button1_Click(object sender, EventArgs e)
47 {
48 string s = "";
49 for (int i = 0; i < this.Repeater1.Items.Count; i++)
50 {
51 //客户端
52 HtmlInputCheckBox chb = (HtmlInputCheckBox)this.Repeater1.Items[i].FindControl("ChkSelect");
53 //CheckBox chb = (CheckBox)this.Repeater1.Items[i].FindControl("CheckBox2"); //服务器端
54 if (chb.Checked == true)
55 {
56 s = s + chb.Value;//chb.Text //服务器端
57 }
58 }
59 Response.Write(s);
60 }
61 }
62 }
63
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)