I stripped this example to make it simple. I have a gridview with a template field. The template field contains two buttons and a label (They must be in the same template field). I want the first button to set the label text to "Win", and the other button to set the label text to "fail". The onrowcommand doesnt seem to be triggered by buttons in a template field. How can I accomplish this?
My gridview code is below:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AutoGenerateColumns="False"
OnRowCommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnWin" runat="server" CommandName="Win" Text="Win" />
<asp:Button ID="btnFail" runat="server" CommandName="Fail" Text="Fail" />
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable myTable = new DataTable();
myTable.Columns.Add("text", typeof(string));
myTable.Rows.Add("First Entry");
myTable.Rows.Add("Second Entry");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
public void Gridview1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//set lblStatus.text to "Win", or "Fail"
}
Thanks in advance!
Here you go...
I see that there is more to this question than is answered here, bear with me. One way would be to delegate the
OnCommand
event of each button to its designated event handler, as follows:Also, as Alison suggests, you won't see the desired output of this unless you use
!IsPostBack
inPage_Load
. Furthermore, on doing so this does in fact enable you to use the one row command event handler as initially suggested, albeit with a slight change in the label retrieval: