asp.net DataList 复选框 也玩 全选反选全不选 by shawl.qiu

本文介绍如何在ASP.NET中实现DataList控件内复选框的全选、反选及全不选功能。通过自定义按钮触发事件,可以轻松地控制每个DataList项内的复选框状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

asp.net DataList 复选框 也玩 全选反选全不选 by shawl.qiu


入乡随俗呀, 既然使用 asp.net 就要使用 asp.net 的模式处理问题...
详细看代码...

shawl.qiu
2007-03-07
http://blog.youkuaiyun.com/btbtd

  1. <%@ Page Language="C#AutoEventWireup="True" %>
  2. <%@ import Namespace="System.Data" %>
  3. <script runat="server">
  4.  void Page_Load(Object s, EventArgs e)
  5.  {
  6.   DataTable dt = new DataTable();
  7.   dt.Columns.Add(new DataColumn("id", typeof(int)));
  8.   DataRow dr;
  9.   
  10.   for(int i=0; i<10; i++)
  11.   {
  12.    dr = dt.NewRow();
  13.    dr[0] = i;
  14.    dt.Rows.Add(dr);
  15.   }
  16.   if(!IsPostBack)
  17.   {
  18.    listAtDataList.DataSource = dt;
  19.    listAtDataList.DataBind();
  20.   }
  21.  } // end Page_Load
  22.  
  23.  private void SelectAllCbxForDataList(DataList dl, string cbxId)
  24.  {
  25.   for(int i=0, j=dl.Items.Count; i<j; i++)
  26.   {
  27.    CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
  28.    cbx.Checked = true;
  29.   }
  30.  } // end private void SelectAllCbxForDataList
  31.  
  32.  private void UnSelectAllCbxForDataList(DataList dl, string cbxId)
  33.  {
  34.   for(int i=0, j=dl.Items.Count; i<j; i++)
  35.   {
  36.    CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
  37.    cbx.Checked = false;
  38.   }
  39.  } // private void UnSelectAllCbxForDataList
  40.  
  41.  private void SelectReverseCbxForDataList(DataList dl, string cbxId)
  42.  {
  43.   for(int i=0, j=dl.Items.Count; i<j; i++)
  44.   {
  45.    CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
  46.    if(cbx.Checked == true)cbx.Checked = false;
  47.    else cbx.Checked = true;
  48.   }
  49.  }
  50.  
  51.  private void CbxSleForDl(Object s, CommandEventArgs e)
  52.  {
  53.   switch(e.CommandName)
  54.   {
  55.    case "all":
  56.     SelectAllCbxForDataList(listAtDataList, "optCheckBox");
  57.     break;
  58.     
  59.    case "none":
  60.     UnSelectAllCbxForDataList(listAtDataList, "optCheckBox");
  61.     break;
  62.     
  63.    case "reverse":
  64.     SelectReverseCbxForDataList(listAtDataList, "optCheckBox");
  65.     break;
  66.   }
  67.  }
  68. </script>
  69. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  70. <html xmlns="http://www.w3.org/1999/xhtml">
  71. <head>
  72. <meta http-equiv="Content-Typecontent="text/html; charset=utf-8" />
  73. <title>shawl.qiu template</title>
  74. </head>
  75. <body>
  76.  <form runat="server">
  77.   <div class="cbxSle algr ">
  78.    <asp:Button id=SelectAllButton runat=server 
  79.     Text="all"
  80.     CommandName = "all"
  81.     OnCommand = CbxSleForDl
  82.     />
  83.    <asp:Button id=UnSelectAllButton runat=server 
  84.     Text="none"
  85.     CommandName = "none"
  86.     OnCommand = CbxSleForDl
  87.     />
  88.    <asp:Button id=SelectReverseButton runat=server 
  89.     Text="reverse"
  90.     CommandName = "reverse"
  91.     OnCommand = CbxSleForDl
  92.     />
  93.   </div
  94.   <br clear="both" />
  95.   
  96.   <ol>
  97.   <asp:DataList id="listAtDataList"
  98.    BorderColor="black"
  99.    CellPadding="5"
  100.    CellSpacing="5"
  101.    RepeatDirection="Horizontal"
  102.    RepeatLayout="Flow"
  103.    RepeatColumns="10"
  104.    ShowBorder="True"
  105.    runat="server">
  106.   
  107.    <HeaderTemplate>
  108.     <h3 class="algr">fan for this</h3>
  109.    </HeaderTemplate>
  110.    
  111.    <HeaderStyle>
  112.    </HeaderStyle>
  113.   
  114.    <AlternatingItemStyle CssClass="atListAlSty">
  115.    </AlternatingItemStyle>
  116.     
  117.    <ItemTemplate
  118.     kkkkkkkkkkk
  119.     <li><asp:CheckBox id=optCheckBox runat=server /></li>
  120.    </ItemTemplate>
  121.   <%--
  122.    <SeparatorTemplate
  123.    </SeparatorTemplate>
  124.   --%>
  125.    <FooterTemplate>
  126.    </FooterTemplate>
  127.   </asp:DataList>
  128.   </ol>

  129.  </form>
  130. </body>
  131. </html>




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值