ASP.NET - How to change text of CommandField Programatically

Asked By Vidyasagar Patil on 28-Jun-11 07:35 AM


  I want to change the Text of the commandfield during runtime
  at present it looks like this

 But there is a column just before it which has two values Comments and No Comments

 If the column has Comments the text of the  CommandField should be EditText="Update Comment" 

 and if it is No Comments then it should display  EditText="Add Comment"

 <asp:CommandField ShowEditButton="true" EditText="Add Comment" />

  how to do this

 Please paste ur code here

Jitendra Faye replied to Vidyasagar Patil on 28-Jun-11 07:40 AM
Write this code in particular event where you want to change the Text-

Button btn = (Button)sender;

GridViewRow r = (GridViewRow)btn.NamingContainer;

(r.FindControl("Id of Deltebutton") as Button).Text = "NewText";

Try an dlet me know.

Ravi S replied to Vidyasagar Patil on 28-Jun-11 07:44 AM

The following code example demonstrates how to use the EditText property to specify a custom caption for an Edit button displayed in a CommandField field of a control.

<%@ Page language="C#" %>

    <form runat="server">
      <h3>CommandField Example</h3>

      <asp:gridview id="CustomersGridView" 
          <asp:commandfield showeditbutton="true"
            edittext="Edit Customer"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
          <asp:boundfield datafield="City"
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"

refer the link also
Vidyasagar Patil replied to Jitendra Faye on 28-Jun-11 08:19 AM

  hi Its not working i dont have the id of the control


 <asp:CommandField ShowEditButton="true" EditText="Add/Update Comment" ItemStyle-Width="95px"

  HeaderStyle-CssClass="tabHead" />

   i want to change the EditText="Add/Update Comment" to EditText="Add Comment" 

 during runtime

Reena Jain replied to Vidyasagar Patil on 28-Jun-11 10:04 AM

To Access and change DetailsView CommandField programmatically, we need to write the following code in the DataBound event of the DetailsView control.

protected void DetailsView1_DataBound(object sender, EventArgs e)
   Button btnobj = (Button)(DetailsView1.Rows[3].Cells[0].Controls[0]);
   ((System.Web.UI.WebControls.Button)(btnobj)).Text= "my edit button";

try this and let me know
Fahad Mirza replied to Reena Jain on 30-Oct-15 07:52 AM
very helpfull..really nice work