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

来源:互联网 发布:淘宝上的铁观音 编辑:程序博客网 时间:2024/05/18 15:51

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


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

shawl.qiu
2007-03-07
http://blog.csdn.net/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>




原创粉丝点击