2

I have a grid view which contains a button in a template field. I want to change the button text when the button click event is completed. Can somebody send me a sample code.

Thanks in advance

1
  • Can we see what you have tried and what errors you are encountering? Commented Mar 24, 2012 at 12:17

1 Answer 1

3

Here is a sample bit of code using the RowCommand() of the GridView.

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lbl1" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND" Text="My Text!" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<string> lst = new List<string>() { "asd", "xxx" };
        GridView1.DataSource = lst;
        GridView1.DataBind();
    }
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "MYCOMMAND")
    {
        Button Button1 = (Button)e.CommandSource;
        if (Button1 != null)
            Button1.Text = "changed text..";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I do not understand what you mean? Did you try my code I posted above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.