Getting DataKeyNames for row on button click

◇◆丶佛笑我妖孽 提交于 2019-12-21 05:54:44

问题


I have a gridview with buttons in each row:

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
}

回答1:


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);
}



回答2:


You can bind CommandName and CommandArgument for your custom Buttons, for exemple:

<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:

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
}



回答3:


You're using the wrong event. Use

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".

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

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.

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

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

...

Yeah, I know.



来源:https://stackoverflow.com/questions/19823803/getting-datakeynames-for-row-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!