關鍵代碼如下:
1.在GridView中Header中添加Checkbox,代碼如下
Code Snippet
- <asp:GridView ID="GridView1" runat="server">
- <Columns>
- <asp:TemplateField>
- <HeaderTemplate>
- <asp:CheckBox ID="HeaderLevelCheckBox" runat="server" />
- </HeaderTemplate>
- <ItemTemplate>
- <asp:CheckBox ID="RowLevelCheckBox" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
2.添加JavaScript檔案
Code Snippet
- <script type="text/javascript">
- function ChangeCheckBoxState(id, checkState) {
- var cb = document.getElementById(id);
- if (cb != null)
- cb.checked = checkState;
- }
-
- function ChangeAllCheckBoxStates(checkState) {
- //更新CheckBoxIDs數組中所有的CheckBox的checkState
- if (CheckBoxIDs != null) {
- for (var i = 0; i < CheckBoxIDs.length; i++)
- ChangeCheckBoxState(CheckBoxIDs[i], checkState);
- }
- }
-
- function ChangeHeaderAsNeeded() {
- //?動更新Header的CheckBox
- if (CheckBoxIDs != null) {
- // 檢查是否所有CheckBox?中
- for (var i = 1; i < CheckBoxIDs.length; i++) {
- var cb = document.getElementById(CheckBoxIDs[i]);
- if (!cb.checked) {
- // 有任何一個CheckBox未?中?則Header的CheckBox不?中
- ChangeCheckBoxState(CheckBoxIDs[0], false);
- return;
- }
- }
- //如果代碼??到???則所有CheckBox?中?則Header的CheckBox也?中
- ChangeCheckBoxState(CheckBoxIDs[0], true);
- }
- }
- </script>
3.為GridView添加 DataBound事件,實現用戶端Header的onclick
的方法
Code Snippet
- protected void GridView1_DataBound(object sender, EventArgs e)
- {
- //每次資料繫結到GridView????建立CheckBoxIDs數組
- //?得header CheckBox
- CheckBox cbHeader = GridView1.HeaderRow.FindControl("HeaderLevelCheckBox") as CheckBox;
- //當點擊header CheckBox 全?/取消 時???用戶端ChangeCheckBoxState函數
- cbHeader.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
- //添加Header CheckBox's ID 到用戶端 CheckBoxIDs 數組
- List<string> ArrayValues = new List<string>();
- ArrayValues.Add(string.Concat("'", cbHeader.ClientID, "'"));
-
- foreach (GridViewRow gvr in GridView1.Rows)
- {
- //?取ItemTemplate中CheckBox
- CheckBox cb = gvr.FindControl("RowLevelCheckBox") as CheckBox;
- //如果Item CheckBox有一?為?中?則Header CheckBox未?中
- cb.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");
- //添加Item CheckBox's ID 到用戶端 CheckBoxIDs 數組
- ArrayValues.Add(string.Concat("'", cb.ClientID, "'"));
- }
- //?出ArrayValues數組到Literal 控制項 (<p><asp:Literal ID="CheckBoxIDsArray" runat="server"></asp:Literal></p>)
- CheckBoxIDsArray.Text = "<script type=\"text/javascript\">" + string.Concat("var CheckBoxIDs = new Array(", string.Join(",", ArrayValues.ToArray()), ");") + "</script>";
- }
4.產生測試資料
Code Snippet
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- DirectoryInfo dirInfo = new DirectoryInfo(Request.PhysicalApplicationPath);
- GridView1.DataSource = dirInfo.GetFiles();
- GridView1.DataBind();
- }
- }
註:翻譯自《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.