GridView中添加一个CheckBox列

本文介绍了一个ASP.NET应用程序中如何使用GridView控件的Checkbox列实现全选功能及记录所选项目的具体方法。通过JavaScript实现了全选功能,并利用C#后台代码收集并处理选中的项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_CheckBoxColumn.aspx.cs" Inherits="GridSamples_GridView_CheckBoxColumn" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
 6None.gif<head runat="server">
 7None.gif    <title>无标题页</title>
 8ExpandedBlockStart.gifContractedBlock.gif    <script language="javascript" type="text/javascript">dot.gif
 9InBlock.gif    function selectAll(obj)
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        var theTable  = obj.parentElement.parentElement.parentElement;
12InBlock.gif        var i;
13InBlock.gif        var j = obj.parentElement.cellIndex;
14InBlock.gif        
15InBlock.gif        for(i=0;i<theTable.rows.length;i++)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            var objCheckBox = theTable.rows[i].cells[j].firstChild;
18InBlock.gif            if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
19ExpandedSubBlockEnd.gif        }

20ExpandedSubBlockEnd.gif    }

21ExpandedBlockEnd.gif    
</script>
22None.gif</head>
23None.gif<body>
24None.gif    <form id="form1" runat="server">
25None.gif    <div>
26None.gif        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
27None.gif            DataKeyNames="id" DataSourceID="AccessDataSource1" AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnRowDataBound="GridView1_RowDataBound">
28None.gif            <Columns>
29None.gif                <asp:TemplateField>
30None.gif                    <ItemTemplate>
31None.gif                        <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' />
32None.gif                    </ItemTemplate>
33None.gif                    <HeaderTemplate>
34None.gif                        &nbsp;<input id="CheckAll" type="checkbox" onclick="selectAll(this);" />本页全选
35None.gif                    </HeaderTemplate>
36None.gif                </asp:TemplateField>
37None.gif                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
38None.gif                    SortExpression="id" />
39None.gif                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
40None.gif                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
41None.gif                <asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />
42None.gif            </Columns>
43None.gif        </asp:GridView>
44None.gif        &nbsp;
45None.gif      
46None.gif        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
47None.gif            SelectCommand="SELECT [id], [name], [sex], [deptid] FROM [employees]"></asp:AccessDataSource>
48None.gif        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ShowAllSelectedItem" />
49None.gif        <asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox></div>
50None.gif    </form>
51None.gif</body>
52None.gif</html>
53None.gif

 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gif
12None.gifpublic partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
15InBlock.gif    /// 获取或设置选中项的集合
16ExpandedSubBlockEnd.gif    /// </summary>

17InBlock.gif    protected ArrayList SelectedItems
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        get
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            return (ViewState["mySelectedItems"!= null? (ArrayList)ViewState["mySelectedItems"] : null;
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        set
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            ViewState["mySelectedItems"= value;
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29InBlock.gif    protected void Page_Load(object sender, EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        
32ExpandedSubBlockEnd.gif    }

33InBlock.gif
34InBlock.gif
35InBlock.gif    protected void GridView1_DataBinding(object sender, EventArgs e)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
38InBlock.gif        CollectSelected();
39ExpandedSubBlockEnd.gif    }

40InBlock.gif
41InBlock.gif    
42InBlock.gif    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
44InBlock.gif        //这里的处理是为了回显之前选中的情况
45InBlock.gif        if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif            DataRowView row = e.Row.DataItem as DataRowView;
48InBlock.gif            CheckBox cb = e.Row.FindControl("CheckBox1"as CheckBox;
49InBlock.gif            if(this.SelectedItems.Contains(row["id"].ToString()))
50InBlock.gif                cb.Checked = true;
51InBlock.gif            else
52InBlock.gif                cb.Checked = false;
53ExpandedSubBlockEnd.gif        }

54ExpandedSubBlockEnd.gif    }

55ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
56InBlock.gif    /// 从当前页收集选中项的情况
57ExpandedSubBlockEnd.gif    /// </summary>

58InBlock.gif    protected void CollectSelected()
59ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
60InBlock.gif        ArrayList selectedItems = null;
61InBlock.gif        if (this.SelectedItems == null)
62InBlock.gif            selectedItems = new ArrayList();
63InBlock.gif        else
64InBlock.gif            selectedItems = this.SelectedItems;
65InBlock.gif
66InBlock.gif        for (int i = 0; i < this.GridView1.Rows.Count; i++)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
68InBlock.gif            string id = this.GridView1.Rows[i].Cells[1].Text;
69InBlock.gif            CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1"as CheckBox;
70InBlock.gif            if (selectedItems.Contains(id) && !cb.Checked)
71InBlock.gif                selectedItems.Remove(id);
72InBlock.gif            if (!selectedItems.Contains(id) && cb.Checked)
73InBlock.gif                selectedItems.Add(id);
74ExpandedSubBlockEnd.gif        }

75InBlock.gif        this.SelectedItems = selectedItems;
76ExpandedSubBlockEnd.gif    }

77InBlock.gif
78InBlock.gif    protected void Button1_Click(object sender, EventArgs e)
79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
80InBlock.gif        //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
81InBlock.gif        CollectSelected();
82InBlock.gif
83InBlock.gif        this.TextBox1.Text = string.Empty;
84InBlock.gif        foreach (object tmp in this.SelectedItems)
85InBlock.gif            this.TextBox1.Text += tmp.ToString() + ",";
86ExpandedSubBlockEnd.gif    }

87ExpandedBlockEnd.gif}

88None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值