利用JavaScript實現GridView中表頭CheckBox的全選功能

來源:互聯網
上載者:User

關鍵代碼如下:

1.在GridView中Header中添加Checkbox,代碼如下

Code Snippet
  1. <asp:GridView ID="GridView1" runat="server">
  2.     <Columns>
  3.         <asp:TemplateField>
  4.             <HeaderTemplate>
  5.                 <asp:CheckBox ID="HeaderLevelCheckBox" runat="server" />
  6.             </HeaderTemplate>
  7.             <ItemTemplate>
  8.                 <asp:CheckBox ID="RowLevelCheckBox" runat="server" />
  9.             </ItemTemplate>
  10.         </asp:TemplateField>
  11.     </Columns>
  12. </asp:GridView>

2.添加JavaScript檔案

Code Snippet
  1. <script type="text/javascript">
  2.     function ChangeCheckBoxState(id, checkState) {
  3.         var cb = document.getElementById(id);
  4.         if (cb != null)
  5.             cb.checked = checkState;
  6.     }
  7.     function ChangeAllCheckBoxStates(checkState) {
  8.         //更新CheckBoxIDs數組中所有的CheckBox的checkState
  9.         if (CheckBoxIDs != null) {
  10.             for (var i = 0; i < CheckBoxIDs.length; i++)
  11.                 ChangeCheckBoxState(CheckBoxIDs[i], checkState);
  12.         }
  13.     }
  14.     function ChangeHeaderAsNeeded() {
  15.         //?動更新Header的CheckBox
  16.         if (CheckBoxIDs != null) {
  17.             // 檢查是否所有CheckBox?中
  18.             for (var i = 1; i < CheckBoxIDs.length; i++) {
  19.                 var cb = document.getElementById(CheckBoxIDs[i]);
  20.                 if (!cb.checked) {
  21.                     // 有任何一個CheckBox未?中?則Header的CheckBox不?中
  22.                     ChangeCheckBoxState(CheckBoxIDs[0], false);
  23.                     return;
  24.                 }
  25.             }
  26.             //如果代碼??到???則所有CheckBox?中?則Header的CheckBox也?中
  27.             ChangeCheckBoxState(CheckBoxIDs[0], true);
  28.         }
  29.     }
  30. </script>

 

3.為GridView添加 DataBound事件,實現用戶端Header的onclick的方法

Code Snippet
  1. protected void GridView1_DataBound(object sender, EventArgs e)
  2. {
  3.     //每次資料繫結到GridView????建立CheckBoxIDs數組
  4.     //?得header CheckBox
  5.     CheckBox cbHeader = GridView1.HeaderRow.FindControl("HeaderLevelCheckBox") as CheckBox;
  6.     //當點擊header CheckBox 全?/取消 時???用戶端ChangeCheckBoxState函數
  7.     cbHeader.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
  8.     //添加Header CheckBox's ID 到用戶端 CheckBoxIDs 數組
  9.     List<string> ArrayValues = new List<string>();
  10.     ArrayValues.Add(string.Concat("'", cbHeader.ClientID, "'"));
  11.     foreach (GridViewRow gvr in GridView1.Rows)
  12.     {
  13.         //?取ItemTemplate中CheckBox
  14.         CheckBox cb = gvr.FindControl("RowLevelCheckBox") as CheckBox;
  15.         //如果Item CheckBox有一?為?中?則Header CheckBox未?中
  16.         cb.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");
  17.         //添加Item CheckBox's ID 到用戶端 CheckBoxIDs 數組
  18.         ArrayValues.Add(string.Concat("'", cb.ClientID, "'"));
  19.     }
  20.     //?出ArrayValues數組到Literal 控制項 (<p><asp:Literal ID="CheckBoxIDsArray" runat="server"></asp:Literal></p>)
  21.     CheckBoxIDsArray.Text = "<script type=\"text/javascript\">" + string.Concat("var CheckBoxIDs = new Array(", string.Join(",", ArrayValues.ToArray()), ");") + "</script>";
  22. }

4.產生測試資料

Code Snippet
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     if (!IsPostBack)
  4.     {
  5.         DirectoryInfo dirInfo = new DirectoryInfo(Request.PhysicalApplicationPath);
  6.         GridView1.DataSource = dirInfo.GetFiles();
  7.         GridView1.DataBind();
  8.     }
  9. }

註:翻譯自《Checking All CheckBoxes in a GridView Using Client-Side Script and a Check All CheckBox》原文代碼下載Download the code used in this article

參考:1.Checking All CheckBoxes in a GridView

2.Working with Client-Side Script 擷取更多關於 client-side HTML attributes and events using server-side ASP.NET code.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.