Click here to Skip to main content
14,391,232 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 ?

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">
Aravindba 20-Feb-14 1:55am
Hi actually i paste as text and again i cut that code and i choose html format in code tab and paste it ,so it change some places,sorry i forgot to see onetime..after post,bcz i will choose html most of place it appear correctly in preview so i click submit button.Now updated...
No problem. the code is fine now. :)

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100