Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am adding link button dynamically to a gridview.

LinkButton av = new LinkButton();

On Page_init()
protected void Page_Init(object sender, EventArgs e)
            av.ID = "lnkAssetView";
            av.Text = "LinkButton";
            av.Width = 15;
            av.Click += new EventHandler(lnk_Click);

EventHandler for LinButton
protected void lnk_Click(object sender, EventArgs e)
 // My code

Gridview OnRowDataBound

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
      if (e.Row.RowType == DataControlRowType.DataRow)

When I click on LinkButton, postback is not happening, and linkbutton got disappears.
Please help me how to fix this.
Thanks in advance

There is no need to generate Click Event for Linkbutton just Set CommandName property of your LinkButton as 'MyCommand' and use RowCommand Event of GridView.
write following code in RowCommand Event to check whether the link button is clicked or not.
   if(e.CommandName == "MyCommand")
      //do the operation here
Share this answer
Why u add link button dynamicall in grid view,why u not try to add link button for one field like if u have 3 colums in grid view like ID,Name and Address. in Name column u can add link button. Textbox used show value and link button used for edit users for further process, try my code
  <asp:TemplateField HeaderText="Name">
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument='<%#Bind("UserId")%>'  CommandName="EditUser" Text='<%# Bind("Name") %>'>LinkButton</asp:LinkButton>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />

Protected Sub gvAll_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAll.RowCommand
        If e.CommandName = "EditUser" Then
'Ur operation codings
End If
    End Sub

in above grid view code i will add link button for name field,so it show Name with link button,if u click name it act as link button.

Note : u not mention for use of link button,for delete,edit ? in my eg used for edit users in grid view,if u want delete u need to use separate link pls mention for what purpose u use ?

Share this answer
There are some problems with the code while you posted. Please correct it.
Aravindba 20-Feb-14 1:07am    
Actually i will post this code form working one,i am use same code in my website,this correct code,what u find problem in this code ? i am not post full gridview code,just post only one column like link button use in gridview,i am not post full code and not spoil this member future like copy and paste coding .
See what is becomes after posting.
Aravindba 20-Feb-14 1:16am    
where , i will change my description i will mention as ID now chnage to Name and i remove comment line.can show where error ?
Check the below line...
<asp:templatefield headertext="Name" xmlns:asp="#unknown">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900