Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,

In my Project i am creating some linkbuttons dynamically when i iterate through my DB table. I am creating the buttons and adding event handler as follows
VB
'Iterating through the Table
If (ds.Tables(0).Rows.Count > 0) Then
                'Reading row by row from DB
                Dim ros As Int32 = ds.Tables(0).Rows.Count - 1
                Do While ros >= 0

                    Dim tr As TableRow = New TableRow()
                    Dim tc As TableCell = New TableCell()
                    Dim btnUser As LinkButton = New LinkButton()
                    AddHandler btnUser.Click, AddressOf btnUser_Click
                    btnUser.ID = (ds.Tables(0).Rows(ros)("UserName")).ToString
                    tc.Controls.Add(btnUser)
                    tr.Cells.Add(tc)
                    tbl.Rows.Add(tr)

                    ros = ros - 1
                Loop
End If


The event handler i have given is as follows
VB
Private Sub btnUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       'setting the session value
       Dim btn As LinkButton = sender
       Dim newsId As String = btn.ID.ToString
       Session("SearchedUserName") = newsId

   End Sub

But while clicking on the linkbutton at runtime,the handler function is not getting called. I am not able to understand where i made mistake. Anybody help me in this please.
Posted

What event in the lifecycle is that code in? You have to add controls dynamically, I believe, in the Init or PreInit page events.

Add your controls in either of these events then only you'll able to handle click event.
Try this:
http://www.aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx[^]



--Amit
 
Share this answer
 
Comments
Аslam Iqbal 8-Aug-12 4:37am    
U got the point. my 5
_Amy 8-Aug-12 4:51am    
Thanks Aslam. :)
L.Abraham 8-Aug-12 4:44am    
The code is in the Click event of a button. and the buttons are displaying in a contentplaceholder.When i click on a search button, i need to get the list of linkbuttons according to the serach query. when i click on search button, i am getting the link buttons according to the query, but if i click on one link button, the event is not getting called...
Hi ,
Check this
not firing dynamically cretated button click event[^]
Best Regards
M.Mitwalli
 
Share this answer
 
Comments
L.Abraham 8-Aug-12 5:06am    
Hi Mohamed,
i have gone through that link. still i am not able to find out where i went wrong... i did the code according to the article given in http://aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx
but still my code is not working... creating linkbutton dynamically is working but it is not triggering the click event
Mohamed Mitwalli 8-Aug-12 5:56am    
The problem when you click on your imagebutton it make postback and go for loading page event and this makes your dynamic control vanish unless you write your code in page_load so my advice you have to make flag in your pag_load like the example i have gave it to you .
Prasad_Kulkarni 16-Aug-12 2:13am    
+5!
Mohamed Mitwalli 16-Aug-12 4:29am    
Thank you Prasad

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