asp.net DataList 复选框 也玩 全选反选全不选 by shawl.qiu
入乡随俗呀, 既然使用 asp.net 就要使用 asp.net 的模式处理问题...
详细看代码...
shawl.qiu
2007-03-07
http://blog.youkuaiyun.com/btbtd
- <%@ Page Language="C#" AutoEventWireup="True" %>
- <%@ import Namespace="System.Data" %>
- <script runat="server">
- void Page_Load(Object s, EventArgs e)
- {
- DataTable dt = new DataTable();
- dt.Columns.Add(new DataColumn("id", typeof(int)));
- DataRow dr;
- for(int i=0; i<10; i++)
- {
- dr = dt.NewRow();
- dr[0] = i;
- dt.Rows.Add(dr);
- }
- if(!IsPostBack)
- {
- listAtDataList.DataSource = dt;
- listAtDataList.DataBind();
- }
- } // end Page_Load
- private void SelectAllCbxForDataList(DataList dl, string cbxId)
- {
- for(int i=0, j=dl.Items.Count; i<j; i++)
- {
- CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
- cbx.Checked = true;
- }
- } // end private void SelectAllCbxForDataList
- private void UnSelectAllCbxForDataList(DataList dl, string cbxId)
- {
- for(int i=0, j=dl.Items.Count; i<j; i++)
- {
- CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
- cbx.Checked = false;
- }
- } // private void UnSelectAllCbxForDataList
- private void SelectReverseCbxForDataList(DataList dl, string cbxId)
- {
- for(int i=0, j=dl.Items.Count; i<j; i++)
- {
- CheckBox cbx = (CheckBox)dl.Items[i].FindControl(cbxId);
- if(cbx.Checked == true)cbx.Checked = false;
- else cbx.Checked = true;
- }
- }
- private void CbxSleForDl(Object s, CommandEventArgs e)
- {
- switch(e.CommandName)
- {
- case "all":
- SelectAllCbxForDataList(listAtDataList, "optCheckBox");
- break;
- case "none":
- UnSelectAllCbxForDataList(listAtDataList, "optCheckBox");
- break;
- case "reverse":
- SelectReverseCbxForDataList(listAtDataList, "optCheckBox");
- break;
- }
- }
- </script>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>shawl.qiu template</title>
- </head>
- <body>
- <form runat="server">
- <div class="cbxSle algr ">
- <asp:Button id=SelectAllButton runat=server
- Text="all"
- CommandName = "all"
- OnCommand = CbxSleForDl
- />
- <asp:Button id=UnSelectAllButton runat=server
- Text="none"
- CommandName = "none"
- OnCommand = CbxSleForDl
- />
- <asp:Button id=SelectReverseButton runat=server
- Text="reverse"
- CommandName = "reverse"
- OnCommand = CbxSleForDl
- />
- </div>
- <br clear="both" />
- <ol>
- <asp:DataList id="listAtDataList"
- BorderColor="black"
- CellPadding="5"
- CellSpacing="5"
- RepeatDirection="Horizontal"
- RepeatLayout="Flow"
- RepeatColumns="10"
- ShowBorder="True"
- runat="server">
- <HeaderTemplate>
- <h3 class="algr">fan for this</h3>
- </HeaderTemplate>
- <HeaderStyle>
- </HeaderStyle>
- <AlternatingItemStyle CssClass="atListAlSty">
- </AlternatingItemStyle>
- <ItemTemplate>
- kkkkkkkkkkk
- <li><asp:CheckBox id=optCheckBox runat=server /></li>
- </ItemTemplate>
- <%--
- <SeparatorTemplate>
- </SeparatorTemplate>
- --%>
- <FooterTemplate>
- </FooterTemplate>
- </asp:DataList>
- </ol>
- </form>
- </body>
- </html>