2015-05-03 86 views
0

所以我有一个C#web应用程序的GridView,它有一个Buttonfield,并且当单击该按钮时,我需要获取该行的其中一个字段的值并将其存储在某个变量中以便以某种方式进行处理。从buttonField获取GridView数据?

但是,GridView和ButtonField似乎都没有办法做到这一点。

谁能推荐从一个GridView获取数据的方式,或者如果这是不可能的,那做了不同类型的视图提供此功能,同时还显示整个表(例如,不是一个DetailsView)

+0

你在用它吗? emTemplate填充GridView字段? – naveen

回答

0

如果您正在使用asp:TemplateField如下图所示,你可以访问使用该行的内容RowCommand

标记

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'> 
     </asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:ButtonField CommandName="AddCode" Text="Add New"/> 

代码

protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if(e.CommandName == "AddCode") 
    { 
     var clickedButton = e.CommandSource as Button; 
     var clickedRow = clickedButton.NamingContainer as GridViewRow; 
     var rows_lblCode = clickedRow.FindControl("lblCode") as Label; 
     // now you can acccess all the label properties. For example, 
     var temp = rows_lblCode.Text; 

    } 
}