I’m trying to pass a cell value from a gridview to my code behind. Ultimately, I need to change the text of a button depending on the cell value.
Here is the code I have:
My .aspx page:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="gridView1" runat="server" OnRowCommand="gview_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Active Status">
<ItemTemplate>
<asp:Label ID="Active" runat="server" Text='<%# Eval("IsActive") %>'></asp:Label>
<asp:Button runat="server" ID="buttonChangeActiveStatus" CommandName="<%# Eval("IsActive") %>" CommandArgument="<%# Eval("IsActive") %>" onclick="buttonChangeActiveStatus_Click" text=""/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
And my .cs code behind:
protected void gview_RowCommand(Object sender, GridViewRowEventArgs e)
{
for (int i = 0; i <= gridView1.Rows.Count - 1; i++)
{
Button activeButton = (Button)gridView1.Rows[i].FindControl("buttonChangeActiveStatus");
// if (cellValue = true)
// {
//change button text foo
// }
// else
// {
// //change button text to bar
// }
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…