热门标签 | 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);
}
}
}

推荐阅读
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • 本文总结了Java中日期格式化的常用方法,并给出了示例代码。通过使用SimpleDateFormat类和jstl fmt标签库,可以实现日期的格式化和显示。在页面中添加相应的标签库引用后,可以使用不同的日期格式化样式来显示当前年份和月份。该文提供了详细的代码示例和说明。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
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社区 版权所有