gridview 全选及其选择项ID的传值

图:

                         全选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全选

复制代码
function sa() {

$(
" #aaa " ).click(function() {
$(
" #GridView1 :checkbox " ).each(function() {  // 遍历所有GridView下面所有的checkbox
$( this ).attr( " checked " true )
});
});
}
< input ID = " aaa "  onclick = " sa() "  type = " checkbox " />
复制代码

后台全选

复制代码
for  ( int  i  = 0 ; i  < this .GridView1.Rows.Count; i ++ )
{
CheckBox cb 
= this .GridView1.Rows[i].Cells[ 1 ].FindControl( " CheckBox1 " as  CheckBox;
// 设定checkbox的选中状态
if  (cb  != null )
{
cb.Checked 
= true ;
}
}
复制代码

===================================================================================

                                                     获取选中的id

方法一:隐藏label

首先应该现在前台gridview上建立一个模板,如建立在最前面,在模板上放一个label并将这个模板设置为隐藏

在这个label上绑定数据id

< asp:TemplateField Visible = " false " >

< ItemTemplate >
< asp:Label ID = " Label2 "  runat = " server "  Text = ' <%#Eval("UserID") %> ' ></ asp:Label >
</ ItemTemplate >
</ asp:TemplateField >

后台获取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行第一个格子上的label控件,即是刚才绑定的label
Label lb  = this .GridView1.Rows[i].Cells[ 0 ].FindControl( " Label2 " as  Label;
// 将label的值取出来用“,”号分开
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")%>' />

必须为前台控件,且必须设置name;因为这里设置的name即为后台用request获取的索引

string s = Request["Chec1"].ToString();

这句代码就把所有选中checkbox的绑定id赋值到s上

如:选中一个  则s为一个id

  选中多个,则s为多个id且以逗号分隔

哈哈,方法二简单吧!

==========================================================================================   

                  完整前后台代码(两种方法共同存在)

前台:

复制代码
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " Default.aspx.cs "  Inherits = " WebApplication4._Default " %>

<! 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 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代码共同应用 --%>
<%-- < input id = " chkAll "  onclick = " javascript:SelectAllCheckboxes(this); "  runat = " server "
type
= " checkbox " />--%>
<%--  jquery全选,和上面的jquery代码呼应 --%>
< input ID = " aaa "  onclick = " sa() "  type = " checkbox " />
</ HeaderTemplate >
< ItemTemplate >
<%-- // 后台label方法遍历id时遍历的checkbox--%>
label遍历:  < asp:CheckBox ID = " CheckBox1 "  runat = " server " />

<%-- // request方法取值时调用的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 >
复制代码

后台:

复制代码
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
{
public partial class  _Default : System.Web.UI.Page
{
protected void  Page_Load( object  sender, EventArgs e)
{
if  ( ! Page.IsPostBack)
{
DataLoad();
}
}
public void  DataLoad()
{
this .GridView1.DataSource  =  Table();
this .GridView1.DataBind();
}

public static  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;
}
}

protected void  Button1_Click1( object  sender, EventArgs e)
{
Response.Write(
" request取id " );
// 获取checkbox绑定的id;以name名称获取请求
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行第一个格子上的label控件,即是刚才绑定的label
Label lb  = this .GridView1.Rows[i].Cells[ 0 ].FindControl( " Label2 " as  Label;
// 将label的值取出来用“,”号分开
selectId  =  selectId  + " , " +  lb.Text;
}
}
}
if  (selectId.Length  > 1 )
{
selectId 
=  selectId.Substring( 1 );
}
Response.Write(selectId);
}
}
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值