How to change link button text in gridview at runtime in asp.net

1.3k Views Asked by At

In my gridview some rows are already presents. Gridview have one column containing link button. But when I add new row in gridview I want to change the text of that link button for that new row only.

Thanks in advance.

2

There are 2 best solutions below

0
On

GridView doesn't support inserting a new row like the way we update/Delete a row.This means that There is NO such BuiltIn functionality for Inserting a row. Obviously there are workarounds for this problem. You may say there is a RowCreated Event of GridView, however this event is raised when GridView renders itself and is seen closely related to RowDataBound.

The easiest way to get hands on inserting a new row is using the DetailsView.

DetailsView:

Add a CommandField inside <Fields> section of Details view as below:

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"   
            AutoGenerateRows="False" DataKeyNames="CustomerID"   
            DataSourceID="SqlDataSource1" Height="50px" Width="125px">  
            <Fields>  
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID"             
                    SortExpression="CustomerID" />  
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName"/>  
                <asp:BoundField DataField="ContactName" HeaderText="ContactName"/>    
                <asp:CommandField ShowDeleteButton="False" ShowEditButton="True"   
                    ShowInsertButton="True" />  
            </Fields>  
        </asp:DetailsView> 

You also need to have a SqlDataSource control as below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"   
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"   
            InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName]) VALUES (@CustomerID, @CompanyName, @ContactName)" >
<InsertParameters>  
                <asp:Parameter Name="CustomerID" Type="String" />  
                <asp:Parameter Name="CompanyName" Type="String" />  
                <asp:Parameter Name="ContactName" Type="String" />
</InsertParameters>  
        </asp:SqlDataSource> 

However, still if you want to use grid View only using some workarounds, below link is worth seeing: http://aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx

0
On

Look at your gridview events, you can access specific row attributes in the event handlers.

For example, RowCreated: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated.aspx