GridView控件:获取datakey的行上按一下按钮 [英] GridView: Get datakey of the row on button click

查看:117
本文介绍了GridView控件:获取datakey的行上按一下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个GridView行的DataKeyName的值时,我有一排有一个onclick事件里面的按钮。在我的按钮我OnClick事件我想那里的按钮所在行的的DataKeyName。

这可能吗?

 < ASP:GridView控件ID =myGridView运行=服务器>
     <柱体和GT;
          < ASP:的TemplateField的HeaderText =列1>
               <&ItemTemplate中GT;
                   ...一堆HTML codeS的
                   < ASP:按钮的ID =myButton的UseSubmitBehavior =假=服务器文本=点击我的onclick =btnClick_Click/>
               < / ItemTemplate中>
           < / ASP:的TemplateField>
     < /专栏>
< / ASP:GridView的>

在我的codebehind

 保护无效btnClick_Click(对象发件人,EventArgs的发送)
{
    //我怎样才能得到该按钮被点击行的DataKeyName价值?
}


解决方案

当我与一个GridView的工作,我通常不使用按钮的OnClick 事件。相反,我使用的GridView中 OnRowCommand 事件和数据绑定到按钮的 CommandArgument 属性。你可以从事件处理程序的 GridViewCommandEventArgs 参数命令参数。

您可以使用的CommandName 参数绑定任意的字符串每个按钮的任何不同的各种的区别开来。请注意,某些命令名称已经用于其他活动,如编辑,更新,取消,选择和删除。

更新:

下面是一个例子,假设数据密钥被称为ID:

 < ASP:GridView控件ID =myGridView=服务器OnRowCommand =myGridView_RowCommand>
     <柱体和GT;
          < ASP:的TemplateField的HeaderText =列1>
               <&ItemTemplate中GT;
                   ...一堆HTML codeS的
                   < ASP:按钮的ID =myButton的=服务器文本=点击我的CommandName =ClickMeCommandArgument ='<%#的eval(ID)%GT;' />
               < / ItemTemplate中>
           < / ASP:的TemplateField>
     < /专栏>
< / ASP:GridView的>

和在code-背后:

 保护无效myGridView_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    VAR datakey = e.CommandArgument;
    // ...
}

How can I get a value of the DataKeyName of a GridView Row when I have a button inside a row that have an OnClick event. In my OnClick event of my button I want to get a the DataKeyName of the row where the button resides.

Is this possible?

<asp:GridView ID="myGridView" run="server">
     <Columns>
          <asp:TemplateField HeaderText="Column 1">
               <ItemTemplate>
                   ... bunch of html codes
                   <asp:Button ID="myButton" UseSubmitBehavior="false" runat="server" Text="Click Me" onclick="btnClick_Click" />
               </ItemTemplate>
           </asp:TemplateField>
     </Columns>
</asp:GridView>

In my codebehind

protected void btnClick_Click(object sender, EventArgs e)
{
    // How can I get the DataKeyName value of the Row that the Button was clicked?
}

解决方案

When I'm working with a GridView, I usually do not use the OnClick event for buttons. Instead, I use the OnRowCommand event on the GridView, and bind data to the CommandArgument property of the button. You can retrieve the command argument from the GridViewCommandEventArgs parameter of the event handler.

You can use the CommandName parameter to bind an arbitrary string to distinguish between any different kinds of buttons you have. Note that some command names are already used for other events, such as "Edit", "Update", "Cancel", "Select" and "Delete".

Update:

Here is an example, assuming the data key is called "ID":

<asp:GridView ID="myGridView" runat="server" OnRowCommand="myGridView_RowCommand">
     <Columns>
          <asp:TemplateField HeaderText="Column 1">
               <ItemTemplate>
                   ... bunch of html codes
                   <asp:Button ID="myButton" runat="server" Text="Click Me" CommandName="ClickMe" CommandArgument='<%# Eval("ID") %>' />
               </ItemTemplate>
           </asp:TemplateField>
     </Columns>
</asp:GridView>

And in the code-behind:

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    var datakey = e.CommandArgument;
    // ...
}

这篇关于GridView控件:获取datakey的行上按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆