2012-09-05 33 views
0

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

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

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

回答

0

您需要处理在GridView中OnRowCommand事件像这样:

<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 
    } 
}  
+0

thx,那有效。 – user1649498