gridview checkbox從伺服器端和用戶端兩個方面實現全選和反選

來源:互聯網
上載者:User

標籤:

GridView中的checkbox的全選和反選在很多的地方都是要求實現的,所以下面就從伺服器端和用戶端兩個方面實現了checkbox的選擇,感興趣的朋友可以瞭解下,希望本文對你有所協助

GridView中的checkbox的全選和反選在很多的地方都是要求實現的,所以下面就從伺服器端和用戶端兩個方面實現了checkbox的選擇。

1.伺服器端

html代碼如下:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="CheckAll" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" AutoPostBack="true" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="num" HeaderText="num" SortExpression="num" /> </Columns> </asp:GridView> 其中關於資料的擷取不是重點,所以就選擇了使用sqldatasource控制項來實現資料的擷取。 GridView中使用了BoundField綁定了ID,num這兩個資料表的欄位。在上面的HTML代碼中,使用了模板列 <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="CheckAll" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" AutoPostBack="true" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> 
View Code

header部分是全選的checkbox,item的部分是單個的checkbox部分。 (請注意AutoPostBack要設定為true,要不然無法觸動伺服器端的代碼)

具體的背景代碼: 

 protected void CheckAll_CheckedChanged(object sender,EventArgs e) { CheckBox ck = sender as CheckBox; if (ck != null) { System.Web.UI.WebControls.GridView g = ck.NamingContainer.NamingContainer as System.Web.UI.WebControls.GridView; for (Int32 i = 0; i < g.Rows.Count; i++) { (g.Rows[i].FindControl("CheckBox1") as CheckBox).Checked = ck.Checked; } } } protected void CheckBox1_CheckedChanged(object sender,EventArgs e) { var count = 0; CheckBox ck = sender as CheckBox; if (ck != null) { System.Web.UI.WebControls.GridView g = ck.NamingContainer.NamingContainer as System.Web.UI.WebControls.GridView; for (Int32 i = 0; i < g.Rows.Count; i++) { if ((g.Rows[i].FindControl("CheckBox1") as CheckBox).Checked) { count++; } } (g.HeaderRow.FindControl("CheckAll") as CheckBox).Checked =count==g.Rows.Count; } } 
View Code

運行頁面以後,可以看到點擊全選的checkbox,可以選擇全部。取消了全選的checkbox,那所以的checkbox也取消選中。如果單個的checkbox全選中一個,那全選的checkbox也選中。如果有一個單個的checkbox沒有選中,那全選的checkbox也不選中。 

2.用戶端的實現: 

html代碼部分,請去除掉兩個checkbox的OnCheckedChanged和AutoPostBack。其他的不變。 

 <script type="text/javascript"> $(function() { $("#GridView1 :checkbox").eq(0).click(function() { if ($(this).is(":checked")) { $(this).parent().parent().nextAll().find(":checkbox").attr("checked", "checked"); } else { $(this).parent().parent().nextAll().find(":checkbox").removeAttr("checked"); } }); $("#GridView1 :checkbox").not($("#GridView1 :checkbox:first")).click(function() { if ($(this).is(":checked")) { if ($("#GridView1 :checked").length == $("#GridView1 :checkbox").length - 1) { $("#GridView1 :checkbox").eq(0).attr("checked", "checked"); } } else { $("#GridView1 :checkbox").eq(0).removeAttr("checked"); } }); }); </script> 
View Code

3.普通的html介面的全選與反選

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="jquery-1.7.1.min.js"></script>    <script>        //複選框事件          //全選、取消全選的事件          function selectAll() {            if ($("#SelectAll").attr("checked")) {                $(":checkbox").attr("checked", true);            } else {                $(":checkbox").attr("checked", false);            }        }        //子複選框的事件          function setSelectAll() {            //當沒有選中某個子複選框時,SelectAll取消選中              if (!$("#subcheck").checked) {                $("#SelectAll").attr("checked", false);            }            var chsub = $("input[type=‘checkbox‘][id=‘subcheck‘]").length; //擷取subcheck的個數              var checkedsub = $("input[type=‘checkbox‘][id=‘subcheck‘]:checked").length; //擷取選中的subcheck的個數              if (checkedsub == chsub) {                $("#SelectAll").attr("checked", true);            }        }    </script></head><body>    <input type="checkbox" id="SelectAll" value="全選" onclick="selectAll();" />    <input type="checkbox" id="subcheck" value="1" onclick="setSelectAll();" />    <input type="checkbox" id="subcheck" value="2" onclick="setSelectAll();" />    <input type="checkbox" id="subcheck" value="3" onclick="setSelectAll();" />    <input type="checkbox" id="subcheck" value="4" onclick="setSelectAll();" />  </body></html>
View Code

 

gridview checkbox從伺服器端和用戶端兩個方面實現全選和反選

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.