如何在DataGrid控件中添加CheckBox控件列

 

前台:

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

 

<% @ Page language = " c# "  Codebehind = " DataGridCheckBox.aspx.cs "  AutoEventWireup = " false "  Inherits = " CommonFunction.DataGridCheckBox "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
< HTML >
    
< HEAD >
        
< title > DataGridCheckBox </ title >
        
< meta content = " Microsoft Visual Studio .NET 7.1 "  name = " GENERATOR " >
        
< meta content = " C# "  name = " CODE_LANGUAGE " >
        
< meta content = " JavaScript "  name = " vs_defaultClientScript " >
        
< meta content = " http://schemas.microsoft.com/intellisense/ie5 "  name = " vs_targetSchema " >
    
</ HEAD >
    
< body MS_POSITIONING = " GridLayout " >
        
< form id = " Form1 "  method = " post "  runat = " server " >
            
< FONT face = " 宋体 " >
                
< TABLE id = " Table1 "  cellSpacing = " 1 "  cellPadding = " 1 "  width = " 300 "  border = " 0 " >
                    
< TR >
                        
< TD >< asp:datagrid id = " dgCheckBox "  runat = " server "  GridLines = " Vertical "  CellPadding = " 3 "  BackColor = " White "
                                BorderWidth
= " 1px "  BorderStyle = " None "  BorderColor = " #999999 "  AutoGenerateColumns = " False " >
                                
< SelectedItemStyle Font - Bold = " True "  ForeColor = " White "  BackColor = " #008A8C " ></ SelectedItemStyle >
                                
< AlternatingItemStyle BackColor = " #DCDCDC " ></ AlternatingItemStyle >
                                
< ItemStyle ForeColor = " Black "  BackColor = " #EEEEEE " ></ ItemStyle >
                                
< HeaderStyle Font - Bold = " True "  ForeColor = " White "  BackColor = " #000084 " ></ HeaderStyle >
                                
< FooterStyle ForeColor = " Black "  BackColor = " #CCCCCC " ></ FooterStyle >
                                
< Columns >
                                    
< asp:TemplateColumn >
                                        
< ItemTemplate >
                                            
< asp:CheckBox id = " chkExport "  runat = " server " ></ asp:CheckBox >
                                        
</ ItemTemplate >
                                        
< EditItemTemplate >
                                            
< asp:CheckBox id = " chkExportON "  runat = " server " ></ asp:CheckBox >
                                        
</ EditItemTemplate >
                                    
</ asp:TemplateColumn >
                                    
< asp:BoundColumn DataField = " LastName "  HeaderText = " LastName " ></ asp:BoundColumn >
                                    
< asp:BoundColumn DataField = " FirstName "  HeaderText = " FirstName " ></ asp:BoundColumn >
                                    
< asp:TemplateColumn HeaderText = " City " >
                                        
< ItemTemplate >
                                            
< asp:Label id = lblColumn runat = " server "  Text = ' <%# DataBinder.Eval(Container, "DataItem.City") %> ' >
                                            
</ asp:Label >
                                        
</ ItemTemplate >
                                    
</ asp:TemplateColumn >
                                
</ Columns >
                                
< PagerStyle HorizontalAlign = " Center "  ForeColor = " Black "  BackColor = " #999999 "  Mode = " NumericPages " ></ PagerStyle >
                            
</ asp:datagrid ></ TD >
                    
</ TR >
                    
< TR >
                        
< TD >< asp:button id = " cmdSelectAll "  runat = " server "  Text = " 全选 " ></ asp:button >< asp:button id = " cmdFindSelected "  runat = " server "  Text = " 取得选择的项 " ></ asp:button ></ TD >
                    
</ TR >
                    
< TR >
                        
< TD >< asp:label id = " Message "  runat = " server " > Label </ asp:label ></ TD >
                    
</ TR >
                
</ TABLE >
            
</ FONT >
        
</ form >
    
</ body >
</ HTML >

 

CS文件:

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

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;
using  System.Configuration;
namespace  CommonFunction
{
    
/// <summary>
    
/// DataGridCheckBox 的摘要说明。
    
/// </summary>

    public class DataGridCheckBox : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid dgCheckBox;
        
protected System.Web.UI.WebControls.Button cmdSelectAll;
        
protected System.Web.UI.WebControls.Button cmdFindSelected;
        
protected System.Web.UI.WebControls.Label Message;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
                DataGridDataBind();
        }

        
//进行数据绑定
        private void DataGridDataBind()
        
{
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//创建数据适配器对象
            SqlDataAdapter da = new SqlDataAdapter("select top 5 LastName,FirstName,City from Employees",conn);
            
//创建DataSet对象
            DataSet ds = new DataSet();
            
try
            
{
                
//填充数据集
                da.Fill(ds,"testTable");
                
//进行数据绑定
                dgCheckBox.DataSource = ds.Tables["testTable"];
                dgCheckBox.DataBind();
            }

            
catch(Exception error)
            
{
                
//输出异常信息
                Response.Write(error.ToString());
            }
        
        }



        
Web 窗体设计器生成的代码

        
private void cmdSelectAll_Click(object sender, System.EventArgs e)
        
{
            System.Web.UI.WebControls.CheckBox chkExport;
            
//如果要全选
            if(cmdSelectAll.Text == "全选")
            
{
                
//循环设置DataGrid控件中的项
                foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
                
{
                    
//建立模板列中CheckBox控件的引用
                    chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
                    
//选中
                    chkExport.Checked =true;
                }


                cmdSelectAll.Text 
= "全消";
            }

            
else
            
{
                
foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
                
{
                    
//建立模板列中CheckBox控件的引用
                    chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
                    
//取消
                    chkExport.Checked = false;
                }

                cmdSelectAll.Text 
= "全选";
            }

        }


        
private void cmdFindSelected_Click(object sender, System.EventArgs e)
        
{
            System.Web.UI.WebControls.CheckBox chkExport;
            String sID;
            System.Text.StringBuilder strMsg 
= new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>");
            
//循环取的DataGrid控件中选定项的值
            foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
            
{

                chkExport 
= (CheckBox)oDataGridItem.FindControl("chkExport");
                
//如果选中了则取值
                if(chkExport.Checked)
                
{
                    sID 
= ((Label)(oDataGridItem.FindControl("lblColumn"))).Text;
                    strMsg.Append(sID
+"<br><hr color=red>");
                }

            }

            
//显示选中项的值
            Message.Text = strMsg.ToString();
        }


    }

}

 

另:

<script language="javascript">
       function SelectAll(tempControl)
       {
           //将除头模板中的其它所有的CheckBox取反

            var theBox=tempControl;
             xState=theBox.checked;   

            elem=theBox.form.elements;
            for(i=0;i<elem.length;i++)
            if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
             {
                  if(elem[i].checked!=xState)
                        elem[i].click();
            }
  } 
</script>

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" PageSize="15">
                    <Columns>
                        <asp:TemplateField>
                            <ItemStyle Width="5%" />
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" />
                            </ItemTemplate>
                            <HeaderTemplate>
                                <asp:CheckBox ID="CheckBox3" runat="server" Text="全选" AutoPostBack="false" TextAlign="Left" οnclick="javascript:SelectAll(this);"/>
                            </HeaderTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="编号" DataField="id">
                            <ItemStyle CssClass="hidden" />
                            <HeaderStyle CssClass="hidden" />
                        </asp:BoundField>
                        <asp:BoundField HeaderText="标题" DataField="adTitle" >
                            <ItemStyle Width="20%" />
                        </asp:BoundField>
                        <asp:BoundField HeaderText="类型" DataField="adClass" >
                            <ItemStyle Width="13%" />
                        </asp:BoundField>
                        <asp:BoundField DataField="adDescribeId" HeaderText="所属区域" />
                        <asp:BoundField DataField="adOrderBy" HeaderText="排序">
                            <ItemStyle Width="5%" />
                        </asp:BoundField>
                        <asp:BoundField HeaderText="添加时间" DataField="addTime" >
                            <ItemStyle Width="18%" />
                        </asp:BoundField>
                        <asp:BoundField HeaderText="添加者" DataField="addAddminName" >
                            <ItemStyle Width="18%" />
                        </asp:BoundField>
                        <asp:HyperLinkField HeaderText="编辑" Text="编辑" DataNavigateUrlFields="id" DataNavigateUrlFormatString="AddAD.aspx?id={0}">
                            <ItemStyle Width="7%" />
                        </asp:HyperLinkField>
                    </Columns>
                    <RowStyle HorizontalAlign="Center" />
                    <HeaderStyle BackColor="White" />
                    <PagerSettings Visible="False" />
                </asp:GridView>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值