C# 单击按钮时获取行的 DataKeyNames

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19823803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:56:34  来源:igfitidea点击:

Getting DataKeyNames for row on button click

c#asp.netgridview

提问by David Tunnell

I have a gridview with buttons in each row:

我有一个网格视图,每一行都有按钮:

enter image description here

在此处输入图片说明

The buttons are in a templatefield:

按钮位于模板字段中:

<asp:GridView ID="storyGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
      BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"    
      CellSpacing="2" DataKeyNames="PK_NonScrumStory" DataSourceID="SqlDataSource1">
...
        <asp:TemplateField HeaderText="Actions">
            <ItemTemplate>
                <asp:Button ID="viewHoursButton" runat="server" Text="View Hours" OnClick="viewHoursButton_OnClick" />
                <asp:Button ID="addHoursButton" runat="server" Text="Add Hours" OnClick="addHoursButton_OnClick" />
                <asp:Button ID="editButton" runat="server" Text="Edit" OnClick="editButton_OnClick" />
                <asp:Button ID="deleteButton" runat="server" Text="Delete" OnClick="deleteButton_OnClick" />
            </ItemTemplate>
        </asp:TemplateField>

How do I get the data key name on click?

如何在单击时获取数据键名称?

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    //get PK_NonScrumStory for clicked row
}

采纳答案by David Tunnell

I figured it out:

我想到了:

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow row = btn.NamingContainer as GridViewRow;
    string pk = storyGridView.DataKeys[row.RowIndex].Values[0].ToString();
    System.Diagnostics.Debug.WriteLine(pk);
}

回答by Pxtl

You're using the wrong event. Use

您使用了错误的事件。用

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

GridView.RowCommand - it's an event on the Grid, not on the cell. On the cell, you provide CommandName string = so instead of your OnClick event, you'd have CommandName="ViewHours".

GridView.RowCommand - 它是网格上的事件,而不是单元格上的事件。在单元格上,您提供 CommandName string = 因此您将拥有 CommandName="ViewHours" 而不是您的 OnClick 事件。

Then in your myGridView_RowCommand event handler you get one of these:

然后在您的 myGridView_RowCommand 事件处理程序中,您将获得以下其中一项:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx

and you put in a big switch statement on e.CommandName. Ugly, but it works.

然后在 e.CommandName 上放入一个大的 switch 语句。丑陋,但它的工作原理。

int index = Convert.ToInt32(e.CommandArgument);

that gets you your Row Index. You can use it with myGridView.DataKeys[index] to fetch DataKeys.

这让你得到你的行索引。您可以将它与 myGridView.DataKeys[index] 一起使用来获取 DataKeys。

...

...

Yeah, I know.

是的,我知道。

回答by Fals

You can bind CommandNameand CommandArgumentfor your custom Buttons, for exemple:

您可以绑定CommandNameCommandArgument为您的自定义Buttons,例如:

<asp:Button ID="deleteButton" 
 runat="server" 
 Text="Delete" 
 CommandName="DeleteItem" 
 CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
 OnClick="deleteButton_OnClick" />

Then you should impletement the event storyGridView1_RowCommand, and treat every command:

然后你应该实现事件 storyGridView1_RowCommand,并处理每个命令:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "DeleteItem")
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button 
    // from the Rows collection.
     GridViewRow row = GridView1.Rows[index];

    //Retrieve the key of the row and delete the item

  }
  else if(e.CommandName == "EditItem")
  {
    //edit the item
  }
  //Other commands
}