Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

Changing the text of a GridView buttonfield text dynamically

$
0
0
I have a GridView with a buttonfield column that I am using to edit and save the value of records. The problem I am having is that in the Grid's Row Command event I want to change the button's text from "Edit" to "Save" but my code below doesn't work. Can anyone see what I'm doing wrong?

HTML:

<asp:GridViewID="gvProducts"CssClass="gvOrders"Width="560px"PagerStyle-CssClass="pager"OnRowCommand="gvProducts_RowCommand"HeaderStyle-CssClass="header"RowStyle-CssClass="rows"AllowPaging="true"AutoGenerateColumns="false"runat="server"DataSourceID="ObjectDataSource1"AllowSorting="True"><Columns><asp:buttonfieldbuttontype="Button"HeaderStyle-BackColor="Transparent"commandname="Edit"headertext="Edit"text="Edit"/><asp:buttonfieldbuttontype="Button"HeaderStyle-BackColor="Transparent"commandname="Delete"headertext="Delete"text="Delete"/><asp:BoundFieldHeaderText="Product ID"DataField="ProductID"SortExpression="ProductID"/><asp:BoundFieldHeaderText="Units In Stock"DataField="UnitsInStock"SortExpression="UnitsInStock"/><asp:BoundFieldHeaderText="Units On Order"DataField="UnitsOnOrder"SortExpression="UnitsOnOrder"/><asp:BoundFieldHeaderText="Reorder Level"DataField="ReorderLevel"SortExpression="ReorderLevel"/></Columns></asp:GridView>


Code behind:

protectedvoid gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case"Edit":
            int index = Convert.ToInt32(e.CommandArgument);
            Button myButton = null;
            myButton = (Button)gvProducts.Rows[index].Cells[0].Controls[0];
            myButton.Text = "Save";
            return;
        case"Delete":
            return;
        default:
            return;
    }
}

Viewing all articles
Browse latest Browse all 3938

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>