作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月23日 8点26分11秒

为DataGrid添加CheckBox控件,并实现“全选”功能。这里是实现的例子

VB.NET 版本

CheckBoxDataGrid.aspx

为DataGrid添加CheckBox控件的例子

后代码 CheckBoxDataGrid.aspx.vb

Imports System.Data Imports System.Data.OleDb Public Class CheckBoxDataGrid Inherits System.Web.UI.Page Protected WithEvents cmdSelectAll As System.Web.UI.WebControls.Button Protected WithEvents dgMain As System.Web.UI.WebControls.DataGrid Protected WithEvents cmdFindSelected As System.Web.UI.WebControls.Button Dim oDataView As DataView Protected WithEvents Label1 As System.Web.UI.WebControls.Label Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="_ + Server.MapPath("Test.mdb") #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load 'Put user code to initialize the page here dgMain.Columns(0).HeaderText = "选项" dgMain.Columns(1).HeaderText = "序号" dgMain.Columns(2).HeaderText = "标题" cmdFindSelected.Text = "查看选中的项目" RefreshGrid() If Not Page.IsPostBack Then cmdSelectAll.Text = "全部选中" dgMain.DataBind() End If End Sub #Region "处理多选" Private Sub cmdSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles cmdSelectAll.Click selectAll() End Sub Private Sub selectAll() Dim oDataGridItem As DataGridItem Dim chkExport As System.Web.UI.WebControls.CheckBox If cmdSelectAll.Text = "全部选中" Then For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl("chkExport") chkExport.Checked = True Next cmdSelectAll.Text = "全部不选" Else For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl("chkExport") chkExport.Checked = False Next cmdSelectAll.Text = "全部选中" End If End Sub #End Region #Region "更新DataGrid" Private Sub RefreshGrid() Dim oConnection As OleDbConnection Dim oCommand As OleDbDataAdapter Dim oDataSet As New DataSet() Try Dim sSQL As String = "Select top 5 * from TestTable order by id" oConnection = New OleDbConnection(sConnectionString) oCommand = New OleDbDataAdapter(sSQL.ToString, oConnection) oCommand.Fill(oDataSet, "TestTable") oDataView = New DataView(oDataSet.Tables("TestTable")) dgMain.DataSource = oDataView oConnection.Close() Catch ex As Exception '// Place Error Handling here End Try End Sub #End Region Private Sub cmdFindSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles cmdFindSelected.Click Dim oDataGridItem As DataGridItem Dim chkExport As System.Web.UI.WebControls.CheckBox Dim oExArgs As New System.Collections.ArrayList() Dim sID As String Label1.Text = "" For Each oDataGridItem In dgMain.Items chkExport = oDataGridItem.FindControl("chkExport") If chkExport.Checked Then Label1.Text = "" sID = CType(oDataGridItem.FindControl("lblColumn"), Label).Text oExArgs.Add(sID) Dim i As Integer = 0 For i = 0 To oExArgs.Count - 1 Label1.Text += oExArgs(i) + "," Next End If Next End Sub End Class

C# 版本

CheckBoxDataGrid.aspx

为DataGrid添加CheckBox控件的例子


DataGridCheckBox.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridCheckBox { /// /// DataGridCheckBox 的摘要说明。 /// 【孟宪会之精彩世界】 /// public class DataGridCheckBox : System.Web.UI.Page { protected System.Web.UI.WebControls.Button cmdSelectAll; protected System.Web.UI.WebControls.Button cmdFindSelected; protected System.Web.UI.WebControls.DataGrid dgMain; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; DataView oDataView; string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("../../aspxWeb.mdb.ascx"); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 dgMain.Columns[0].HeaderText = "选项"; dgMain.Columns[1].HeaderText = "序号"; dgMain.Columns[2].HeaderText = "标题"; cmdFindSelected.Text = "查看选中的项目"; RefreshGrid(); if( !this.IsPostBack) { cmdSelectAll.Text = "全部选中"; dgMain.DataBind(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.cmdSelectAll.Click += new System.EventHandler(this.cmdSelectAll_Click); this.cmdFindSelected.Click += new System.EventHandler(this.cmdFindSelected_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void cmdSelectAll_Click(object sender, System.EventArgs e) { selectAll(); } private void selectAll() { System.Web.UI.WebControls.CheckBox chkExport ; if( cmdSelectAll.Text == "全部选中") { foreach(DataGridItem oDataGridItem in dgMain.Items) { chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); chkExport.Checked = true; } cmdSelectAll.Text = "全部不选"; } else { foreach(DataGridItem oDataGridItem in dgMain.Items) { chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); chkExport.Checked = false; } cmdSelectAll.Text = "全部选中"; } } private void RefreshGrid() { OleDbConnection oConnection; OleDbDataAdapter oCommand ; DataSet oDataSet = new DataSet(); try { string sSQL = "Select top 10 * from Document order by CreateDate DESC"; oConnection = new OleDbConnection(sConnectionString); oCommand = new OleDbDataAdapter(sSQL.ToString(), oConnection); oCommand.Fill(oDataSet, "Document"); oDataView = new DataView(oDataSet.Tables["Document"]); dgMain.DataSource = oDataView; oConnection.Close(); } catch(Exception ex) { Label1.Text = ex.Message.ToString(); } } private void cmdFindSelected_Click(object sender, System.EventArgs e) { System.Web.UI.WebControls.CheckBox chkExport; System.Collections.ArrayList oExArgs = new System.Collections.ArrayList(); string sID; Label1.Text = ""; Label2.Text = ""; foreach(DataGridItem oDataGridItem in dgMain.Items) { chkExport = (CheckBox)oDataGridItem.FindControl("chkExport"); if( chkExport.Checked) { //如果要进行删除,可以在这里构造sql语句进行删除 string sql = "DELETE FROM Document WHERE id =" + ((HtmlInputHidden)oDataGridItem.FindControl("SelectedID")).Value; Label2.Text += " " + sql; sID = ((HtmlInputHidden)oDataGridItem.FindControl("SelectedID")).Value; oExArgs.Add(sID); int i = 0; Label1.Text = ""; for( i = 0;i"; } } } Label2.Text += "
执行SQL语句即可删除,这里省略。"; } } }

转载于:https://www.cnblogs.com/goody9807/archive/2006/09/01/492019.html

[收藏]为DataGrid添加CheckBox控件相关推荐

  1. 选择DataGrid中的CheckBox控件后该行背景变色

    在网络开发中,经常遇到需要使用ASP.NET与JavaScript联合进行控制的情况.在本篇中,将使用DataGrid进行数据绑定,使用Javascript控制当选中其中的checkbox时,该行颜色 ...

  2. ASP.NET与JavaScript联合操作之一 选择DataGrid中的CheckBox控件后该行背景变色

    在网络开发中,经常遇到需要使用ASP.NET与JavaScript联合进行控制的情况.在本篇中,将使用DataGrid进行数据绑定,使用Javascript控制当选中其中的checkbox时,该行颜色 ...

  3. ListBox控件、CheckBox控件的多选功能

    我们在使用控件的时候,经常会用到ListBox控件.CheckBox控件来进行选择 1.ListBox控件 我们经常会在网页或程序中看到这样的功能: 将一个ListBox控件里的选择项添加到另一个Li ...

  4. 添加RichEdit控件导致MFC对话框程序无法执行的解决方法

    解决办法: 在应用程序类(App类)中的InitInstance函数体内添加以下语句: AfxInitRichEdit();         对应1.0版本 如果有2.0版本需要添加如下 AfxIni ...

  5. Leaflet中实现添加比例尺控件与自定义版权控件与链接

    场景 Leaflet快速入门与加载OSM显示地图: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/122290880 上面加载显示 ...

  6. Winform中使用DevExpress时给控件添加子控件的方法

    场景 在Winform中使用DevExpress时经常使用PanelControl控件用来进行布局设计,因此需要在代码中生成控件并添加子控件. 实现 一种是设置要添加的自控件的Parent属性为容器控 ...

  7. 在GLSurfaceView上添加Layout控件(android)

    查找了很久,才找出在GLSurfaceView上添加控件的方法.废话不说,本例实现了一个Native opengl es 程序,绘制了一个旋转三角形:当然主题是在GLSurfaceView上添加Lay ...

  8. Scott Mitchell 的ASP.NET 2.0数据教程之三十九:: 在编辑和插入界面里添加验证控件...

    原文 | 下载本教程中的编码例子 | 下载本教程的PDF版 导言 到目前为止的讨论编辑DataList的教程里,没有包含任何验证用户的输入,即使是用户非法输入- 遗漏了product的name或者负的 ...

  9. CheckBox控件

    前台代码: 1 <asp:CheckBox ID="CheckBox1" runat="server" Text ="苹果"/> ...

最新文章

  1. 多线程-010-后台线程
  2. 新的UWP和Win32应用程序分发模型
  3. flask mysql项目模板渲染_Flask模板渲染
  4. 外部网络如何获取网口打印机的ip地址_网络打印机端口用名称好是还是IP好?...
  5. php二维数组中的查找,PHP实现二维数组中的查找算法小结
  6. SqlServer 执行计划及Sql查询优化初探
  7. HTML5清除2个div标签的空白,DIV标签里面IMG图片下方留有空白怎么办
  8. php同时删除两个列表数据库,PHP 处理 数据库多表,既能高效又能思路清晰如何处理的?...
  9. 基于deepin-wine的windows软件打包deb安装包教程
  10. python多线程结束线程_Python线程– Python多线程
  11. SuperPoint学习(一)
  12. 产品开发的生命周期管理
  13. Android-SDK下载及安装配置教程
  14. 机器学习视频推荐-绝对的通俗易懂(线性回归,逻辑回归,朴素贝叶斯分类器,K-近邻,SVM,决策树,随机森林,XGboost,k-means聚类)
  15. 360隐私保险箱 vs misuo
  16. JS正则表达式常见用法实例详解
  17. 怎样用计算机截图,如何在电脑中截图
  18. conda search cuda后没有版本10的问题
  19. 每日刷题记录 (十五)
  20. 同事推荐的GIS书籍

热门文章

  1. linux运行raxml,RAxML安装
  2. C++中的const成员函数介绍
  3. 华为机试第11题python
  4. 神经网络版员工离职预测
  5. java spring cloud版b2b2c社交电商spring cloud分布式微服务-docker-feign-hystrix(六)
  6. Silverlight实用窍门系列:59.多个中心点联动多线的可拖动控件扩展为拓扑图
  7. 2017.4.24 js 中的iscroll
  8. opencv配置(转)
  9. RCP开发小技巧(二)
  10. android - Animation详解