天天看点

Repeater和CheckBox控件(客户端和服务器端)实行全选或多条选择

客服端代码

 1 <%@ page language="c#" autoeventwireup="true" codebehind="repeatercheckbox1.aspx.cs" inherits="checkboxes.repeatercheckbox1" %>

 2

 4

 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;

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

继续阅读