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

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - jierry

ASP.NET2.0控件一览---标准控件(2) ASP.NET2.0控件一览---标准控件(1) 控件开发时两种JS嵌入资源方式的使用 - jierry - 博客园 T-SQL tips(1)临时表和表变量 Flash Control for ASP.NET 2.0-Include Flash movies in your aspx pages 为DataGrid创建自定义列控件(三) - jierry - 博客园 为DataGrid创建自定义列控件(二) 为DataGrid创建自定义列控件(一) (转)SQLServer和Oracle的常用函数对比 《Effective C#》读书笔记(4) 《Effective C#》读书笔记(3) 《Effective C#》读书笔记(2) 《Effective C#》读书笔记(1) 选择合适的数据控件 自带图层的链接控件(DKLinks 1.0.0.323 ) 关于CodeBuild V3.0的一些想法 小工具:SQL存储过程解密修改工具 交叉表应用-成绩统计 现在提供第一版的存储过程生成器下载,欢迎大家试用
为DataGrid创建自定义列控件(四)
jierry · 2005-11-07 · via 博客园 - jierry

        全选和多选的功能在DataGrid中使用的时候很多,以前我们都是创建CheckBox模板列,然后在后台中捕获ChecnkBox的选择情况来实现全选或多选.现在为了加快开发的速度,使用这个CheckBoxColumn列控件,可以很方便的实现多选或全选的功能.
        代码如下:

CheckBoxColumn

        

        看过我前面几篇列控件介绍的朋友对上面的代码一定不会有什么问题.
        首先还是在DataGrid中引用这个列控件,方法和以前一样,这里就不多说了.
        然后看看具体的使用:

  1. 获得选择行的Index值

    CheckBoxColumn chkColumn = (CheckBoxColumn)this.DataGrid1.Columns[0];
                
    foreach(object index in chkColumn.SelectIndexes)//SelectIndexes获得选择的Index值
                {
                    Response.Write(index.ToString()
    +"<br>");
                }

  2. 获得选择行的DataKeys值

    CheckBoxColumn chkColumn = (CheckBoxColumn)this.DataGrid1.Columns[0];
                
    foreach(object index in chkColumn.SelectedDataKeys)//SelectedDataKeys获得选择的DataKeys值
                {
                    Response.Write(index.ToString()
    +"<br>");
                }

  3. 获得未选择行的Index值和获得未选择行的DataKeys值

    CheckBoxColumn chkColumn = (CheckBoxColumn)this.DataGrid1.Columns[0];
                
    foreach(object index in chkColumn.UnSelectIndexes)
                
    {
                    Response.Write(index.ToString()
    +"<br>");
                }




    CheckBoxColumn chkColumn 
    = (CheckBoxColumn)this.DataGrid1.Columns[0];
                
    foreach(object index in chkColumn.UnSelectedDataKeys)
                
    {
                    Response.Write(index.ToString()
    +"<br>");
                }

  4. 全选/取消全选

    foreach(DataGridItem item in this.DataGrid1.Items)
                
    {
                    HtmlInputCheckBox chkBox
    =(HtmlInputCheckBox)item.FindControl("checkboxCol");
                    chkBox.Checked 
    = true;        
                }

    //如果你自定义了列控件的ID,"checkboxCol"换成自定义的ID值

  5. 获得选择行的任意列的

                CheckBoxColumn chkColumn = (CheckBoxColumn)this.DataGrid1.Columns[0];
                
    foreach(object index in chkColumn.SelectIndexes)
                
    {
                    Response.Write(DataGrid1.Items[(
    int)index].Cells[1].Text);
                }


        基本的使用就介绍完了,都非常的简单.当然我们可以在这个列控件的基础上扩展新的功能,比如在Head加入全选/取消选择框

if(itemType == ListItemType.Header)
            
{
                CheckBox headerCheckBox 
= new CheckBox();
                headerCheckBox.ID 
= "chkAll";
                headerCheckBox.CheckedChanged 
+= new EventHandler(this.headerCheckBox_CheckedChanged);
                headerCheckBox.AutoPostBack 
= true;
                headerCheckBox.Text 
= "全选/取消";
                cell.Controls.Add(headerCheckBox);
            }


private void headerCheckBox_CheckedChanged(object sender, EventArgs e)
        
{
            
foreach (DataGridItem item in this.Owner.Items)
            
{
                
//iterate each DataGridItem and find our checkbox
                HtmlInputCheckBox chkBox = (HtmlInputCheckBox) item.FindControl("checkboxCol");
                
//now set that checkboxCol value = to selected
                if(chkBox.Checked == false)
                    chkBox.Checked 
= true;
                
else
                    chkBox.Checked 
= false;

            }

        }