Question

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!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top