0

我正在使用 asp.net、c# 在 Visual Studio 2012 中创建一个网站。我创建了一个网格视图,它从我的 sql 服务器获取数据,并创建了一个绑定到 id_p 的按钮字段,它是从数据库中获取的列之一。我正在尝试获取单击按钮所在行的 id_p 数据。

<asp:ButtonField ButtonType="Button" DataTextField="id_p" 
DataTextFormatString="Stavi u košaricu" Text="Button1" />

我需要的不是选定的行,只有 id_p 值,请问我该怎么做?

4

1 回答 1

0

您需要这样处理OnRowCommandgridview 上的事件:

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView_RowCommand" 

并创建一个 ItemTemplateField 来显示常规<asp:button>而不是使用ButtonField列:

<asp:TemplateField>
     <ItemTemplate>
         <asp:Button ID="btn" runat="server" 
          CommandName="Something" CommandArgument='<%#Eval("id_p") %>'
          Text="Stavi u košaricu" />
     </ItemTemplate>
 </asp:TemplateField>

现在您处理 RowCommand 事件:

protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    string m_id = e.CommandArgument.ToString();
    if (e.CommandName == "Something")
    {
       //do something with m_id
    }
}    
于 2012-09-05T15:59:34.833 回答