如圖: 全選checkbox Js全選checkbox 遍歷所有checkbox function SelectAllCheckboxes(spanChk) {
var oItem = spanChk.children; var theBox = (spanChk.type =="checkbox") ? spanChk : spanChk.children.item[0]; xState = theBox.checked; elm = theBox.form.elements; for (i =0; i < elm.length; i++) if (elm[i].type =="checkbox"&& elm[i].id != theBox.id) { if (elm[i].checked!= xState) elm[i].click(); } } <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox"/> jquery全選 后臺(tái)全選 =================================================================================== 獲取選中的id 方法一:隱藏label 首先應(yīng)該現(xiàn)在前臺(tái)gridview上建立一個(gè)模板,如建立在最前面,在模板上放一個(gè)label并將這個(gè)模板設(shè)置為隱藏 在這個(gè)label上綁定數(shù)據(jù)id 后臺(tái)獲取label的值 string selectId ="";
for (int i =0; i <this.GridView1.Rows.Count; i++) { //找到gridview上的CheckBox控件 CheckBox ch =this.GridView1.Rows[i].Cells[1].FindControl("CheckBox1") as CheckBox; if (ch !=null) { if (ch.Checked) { //找到第i行第一個(gè)格子上的label控件,即是剛才綁定的label Label lb =this.GridView1.Rows[i].Cells[0].FindControl("Label2") as Label; //將label的值取出來用“,”號(hào)分開 selectId = selectId +","+ lb.Text; } } } if (selectId.Length >1) { selectId = selectId.Substring(1); } Response.Write(selectId); 方法二: 直接將值綁定到gridview上的checkbox上 <input type="checkbox" name="Chec1" value='<%#Eval("UserID")%>' /> 必須為前臺(tái)控件,且必須設(shè)置name;因?yàn)檫@里設(shè)置的name即為后臺(tái)用request獲取的索引 string s = Request["Chec1"].ToString(); 這句代碼就把所有選中checkbox的綁定id賦值到s上 如:選中一個(gè) 則s為一個(gè)id 選中多個(gè),則s為多個(gè)id且以逗號(hào)分隔 哈哈,方法二簡(jiǎn)單吧! ========================================================================================== 完整前后臺(tái)代碼(兩種方法共同存在) 前臺(tái): <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title></title> <script src="jquery-1.5.2.min.js" type="text/javascript"></script> <script type="text/javascript"> // function SelectAllCheckboxes(spanChk) { // var oItem = spanChk.children; // var theBox = (spanChk.type == "checkbox") ? spanChk : spanChk.children.item[0]; // xState = theBox.checked; // elm = theBox.form.elements; // for (i = 0; i < elm.length; i++) // if (elm[i].type == "checkbox" && elm[i].id != theBox.id) { // if (elm[i].checked != xState) // elm[i].click(); // } // } function sa() { $("#aaa").click(function() { $("#GridView1 :checkbox").each(function() { $(this).attr("checked", true) }); }); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button"/> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField Visible="false"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%#Eval("UserID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <HeaderTemplate> <%-- javascript全選,以注釋掉,和上面注釋的javascript代碼共同應(yīng)用--%> <%--<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox"/>--%> <%-- jquery全選,和上面的jquery代碼呼應(yīng)--%> <input ID="aaa" onclick="sa()" type="checkbox"/> </HeaderTemplate> <ItemTemplate> <%--//后臺(tái)label方法遍歷id時(shí)遍歷的checkbox--%> label遍歷: <asp:CheckBox ID="CheckBox1" runat="server"/> <%--//request方法取值時(shí)調(diào)用的checkbox--%> request方法: <input type="checkbox" name="Chec1" value='<%#Eval("UserID")%>'/> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UserID" HeaderText="UserID"/> <asp:BoundField DataField="UserName" HeaderText="UserName"/> <asp:BoundField DataField="Station" HeaderText="Station"/> <asp:BoundField DataField="Status" HeaderText="Status"/> </Columns> </asp:GridView> </div> </form> </body> </html> 后臺(tái): using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace WebApplication4 { publicpartialclass _Default : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataLoad(); } } publicvoid DataLoad() { this.GridView1.DataSource = Table(); this.GridView1.DataBind(); } publicstatic DataSet Table() { using (SqlConnection conn =new SqlConnection (ConfigurationManager.ConnectionStrings["learning"].ConnectionString)) { conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText ="select top 10 * from users"; SqlDataAdapter adapter =new SqlDataAdapter(cmd); DataSet ds =new DataSet(); adapter.Fill(ds); return ds; } } protectedvoid Button1_Click1(object sender, EventArgs e) { Response.Write("request取id"); //獲取checkbox綁定的id;以name名稱獲取請(qǐng)求 string s = Request["Chec1"].ToString(); Response.Write(Request["Chec1"].ToString()); //////////////////////////////////////////////////////////////// Response.Write("label綁定遍歷取值取id"); //獲取label綁定的值 string selectId =""; for (int i =0; i <this.GridView1.Rows.Count; i++) { //找到gridview上的CheckBox控件 CheckBox ch =this.GridView1.Rows[i].Cells[1].FindControl("CheckBox1") as CheckBox; if (ch !=null) { if (ch.Checked) { //找到第i行第一個(gè)格子上的label控件,即是剛才綁定的label Label lb =this.GridView1.Rows[i].Cells[0].FindControl("Label2") as Label; //將label的值取出來用“,”號(hào)分開 selectId = selectId +","+ lb.Text; } } } if (selectId.Length >1) { selectId = selectId.Substring(1); } Response.Write(selectId); } } } |
|