所以我有一个C#web应用程序的GridView,它有一个Buttonfield,并且当单击该按钮时,我需要获取该行的其中一个字段的值并将其存储在某个变量中以便以某种方式进行处理。从buttonField获取GridView数据?
但是,GridView和ButtonField似乎都没有办法做到这一点。
谁能推荐从一个GridView获取数据的方式,或者如果这是不可能的,那做了不同类型的视图提供此功能,同时还显示整个表(例如,不是一个DetailsView)
所以我有一个C#web应用程序的GridView,它有一个Buttonfield,并且当单击该按钮时,我需要获取该行的其中一个字段的值并将其存储在某个变量中以便以某种方式进行处理。从buttonField获取GridView数据?
但是,GridView和ButtonField似乎都没有办法做到这一点。
谁能推荐从一个GridView获取数据的方式,或者如果这是不可能的,那做了不同类型的视图提供此功能,同时还显示整个表(例如,不是一个DetailsView)
您可以点击此链接:https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx。
GridView.Rows[index](columnIndex)
如果您正在使用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;
}
}
你在用它吗? emTemplate填充GridView字段? – naveen