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

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
爱范儿
爱范儿
Recent Announcements
Recent Announcements
AI
AI
V
Visual Studio Blog
H
Heimdal Security Blog
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
人人都是产品经理
人人都是产品经理
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
S
Secure Thoughts
S
Security Affairs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
Schneier on Security
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
罗磊的独立博客
V
Vulnerabilities – Threatpost
S
Securelist
N
News and Events Feed by Topic
Cloudbric
Cloudbric
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
小众软件
小众软件

博客园 - 网际浪人

.Net Framework 4.0 中利用Task实现并行处理、串并行混合处理 C# Process调用应用程序失败时应注意的问题 程序员的幽默 To腾讯:强行收集用户个人隐私的行为不可饶恕 VS2005打开VS2008项目的2种方法(转) 【转】Oracle Conversion Functions 晨星、银河基金业绩排行榜数据转换工具 ORACLE纯SQL实现多行合并一行 ASP.NET项目添加Log4Net后,发布后无法写日志 “必应”不应、“谷歌”不歌 你好2009,再见2008,牛年犇犇犇 一种在SQLServer中实现Sequence的高效方法 oledb使用Access更新和插入操作的注意点 5.12大地震——小学二年级小表妹谢可欣的诗 GridView自动排序 [转]SQL Server 2005链接字符串 对HtmlEncode的增强——HtmlEntitiesEncode C#调用Excel VBA宏 封装SoapException处理Webservice异常
GridView中使用DataKeyNames存储数据键值
网际浪人 · 2008-04-09 · via 博客园 - 网际浪人

很多时候我们需要在GridView的RowCommand之类的事件中需要获取当前行的一些关联性的数据值。但这些数据值又没有直接体现在GridView的列中。这个时候该怎么办呢?
有同学喜欢用隐藏列的方式,把需要使用但不显示的字段绑定到此列上,同时设置列宽为0或不显示,使用时可以用常规的取某行某列的方式来获取数据。
但是在Framework 2.0中,我们可以采用DataKeyNames的方式来获取此类数据。

代码示例:
(前台)

        <asp:GridView ID="GridView1" runat="server" DataKeyNames="Grup" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False">
            
<Columns>
                
<asp:TemplateField>
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" runat="server" Text='<%#Eval("GrupName") %>'></asp:Label>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:ButtonField Text="按钮" />
            
</Columns>
        
</asp:GridView>

Grup 为我们想使用但不需要显示的列。(如果有多个字段,使用逗号分开)
(后台)

    protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack )
        
{
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"Grup");
            dt.Columns.Add(
"GrupName");

            dt.Rows.Add(
new object[] 0,"营业部" });
            dt.Rows.Add(
new object[] 1,"市场部" });

            
this.GridView1.DataSource = dt;
            
this.GridView1.DataBind();
        }


    }


    
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    
{
        
// 获取当前行索引 
        int index = Convert.ToInt32(e.CommandArgument);

        
// 取出当前行数据键值对象中的值 
        string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();  
    }

顺便补充一句。
如果你使用模板列中放置按钮控件的方式,要想在按钮事件中获取这种字段值就更简单了。

只需要在按钮的CommandArgument属性设置为想绑定的字段,如:

<asp:TemplateField> 
     
<ItemTemplate> 
         
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" CommandArgument=' <%#Eval("Grup") %>' /> 
     
</ItemTemplate> 
</asp:TemplateField> 


按钮事件中如是写:

protected void Button2_Click(object sender, EventArgs e) 

    
string strGrup = ((Button)sender).CommandArgument.ToString(); 
}