【问题标题】:Getting DataKeyNames for row on button click在按钮单击时获取行的 DataKeyNames
【发布时间】:2013-11-06 22:05:31
【问题描述】:

我有一个每行都有按钮的gridview:

按钮位于模板字段中:

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

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

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我想通了:

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

      您可以为自定义按钮绑定CommandNameCommandArgument,例如:

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

      然后你应该实现事件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
      }
      

      【讨论】:

        【解决方案3】:

        您使用了错误的事件。使用

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

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

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

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

        然后您在 e.CommandName 上添加了一个大的 switch 语句。丑陋,但它有效。

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

        这将为您提供行索引。您可以将它与 myGridView.DataKeys[index] 一起使用来获取 DataKeys。

        ...

        是的,我知道。

        【讨论】:

          猜你喜欢
          • 2023-03-17
          • 1970-01-01
          • 2012-05-14
          • 2019-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多