分享

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

 绚爷好人图书馆 2014-08-20

      如图:

                         全选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./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代码共同应用--%>
<%--<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
{
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名称获取请求
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);
}
}
}
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多