arun arun - 2 months ago 31
ASP.NET (C#) Question

Changing button text of gridview

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

Answer

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..";
    }
}
Comments