热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

如图:全选checkboxJs全选checkbox遍历所有checkboxView

      如图:

                         全选checkbox          

Js全选checkbox

    遍历所有checkbox

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

View Code
function sa() {

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

后台全选

View Code
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

View Code
< asp:TemplateField Visible = " false " >

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

后台获取label的值

View Code
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上

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

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

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

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

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

哈哈,方法二简单吧!

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

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

前台:

View Code
<% @ 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 // 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 >

后台:

View Code
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);
}
}
}

推荐阅读
author-avatar
天使犯罪de快乐
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有