惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 柴火

SqlServer死锁处理存储过程 DB2常用SQL的写法(未整理) 常用快递公司列表 看看google员工的“堕落”工作环境 Eclipse的快捷键大全(不断更新中) [转]不花钱不求人 硬盘数据恢复六大招 [转]大学生不如狗 [转]dos常用命令 [转]net命令详解 [转]FTP命令大全 [转]如何在Web页面上直接打开、编辑、创建Office文档 鼠标指向提示javascript程序 网页或VB中操作word的方法 Script直接写文件内容到word Asp.net中的两种刷新父窗体方法 [转]一个民工的月账本(看了让人流泪) 数据库连接串大全 【Asp.Net】在DataGrid里添加行脚本代码的方法 简单的SQL分页法
【Asp.Net】DataGrid里实现多选提交的方法
柴火 · 2006-07-06 · via 博客园 - 柴火

思路:在DataGrid里建模板列,使用CheckBox和CheckBoxList控件提交,CheckBox控件用作全选,CheckBoxList控件记录商品的编号,用循环收集提交的编号。

1、在DataGrid里建立一个模板列,打开模板,在Header里放一个CheckBox控件,改名为:SelAll。在Item里放一个CheckBoxList控件,改名为:SelNum

2、将SelAll控件的AutoPostBack属性设置为true

3、绑定商品的ID到CheckBoxList控件,在DataGrid的ItemDataBound事件中加入:

if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            
{
                
                CheckBoxList chklist 
= (CheckBoxList)e.Item.Cells[1].FindControl("SelNum");
                
                chklist.Items[
0].Text = "";
                chklist.Items[
0].Value = Convert.ToString( DataBinder.Eval(e.Item.DataItem, "DetailID") );

                
            }

我们只需要CheckBoxList控件的一个组件

4、在DataGrid的ItemCreated事件里创建SelAll的CheckedChanged事件:

if( e.Item.ItemType == ListItemType.Header )
            
{
                CheckBox selall 
= (CheckBox)e.Item.FindControl("SelAll");

                selall.CheckedChanged 
+=new EventHandler(selall_CheckedChanged);
            }

加入SelAll的CheckedChanged事件的方法:

 1protected void selall_CheckedChanged(object sender, System.EventArgs e) 
 2        {
 3
 4            CheckBox chk = this.GetHeaderCheckBox(this.DataGrid1);
 5
 6            foreach ( DataGridItem i in this.DataGrid1.Items )
 7            {
 8
 9                CheckBoxList inChk = (CheckBoxList)i.FindControl("SelNum");
10
11                if ( i.Cells[0].Enabled == true )
12                    inChk.Items[0].Selected = chk.Checked;
13
14            }

15
16        }

行4用到方法GetHeaderCheckBox,作用是用来确定选定状态的。

private CheckBox GetHeaderCheckBox(DataGrid dg)
        
{

            CheckBox chk 
= null;

            
foreach ( DataGridItem i in dg.Controls[0].Controls )
            
{

                
if(i.ItemType == ListItemType.Header)
                
{

                    chk 
= (CheckBox)i.FindControl("SelAll");

                    
break;

                }


            }


            
return chk;

        }

5、最后我们通过一个单击按钮来得到我们需要的ID值。在按钮的Click事件中加入:

for ( int i=0; i<this.DataGrid1.Items.Count; i++ )
            
{
                CheckBoxList chklist 
= (CheckBoxList)this.DataGrid1.Items[i].Cells[1].FindControl("SelNum");

                
if ( chklist.Items[0].Selected )
{                
    Response.Write( chklist.Items[
0].Value );
    Response.Write( 
"<br>" );
}

            }