ASP.NET - How to change text of CommandField Programatically

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


    Hi 

  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

 Thanks
  
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
HI

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 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.80%29.aspx control.


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

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

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        datakeynames="CustomerID"  
        runat="server">
        
        <columns>
          <asp:commandfield showeditbutton="true"
            edittext="Edit Customer"
            canceltext="Discard"
            updatetext="Revise"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
        </columns>
        
      </asp:gridview>
            
      <!-- 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%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>


refer the link also
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandfield.edittext%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandfield.aspx
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
hi,

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