Dynamic Buttons in Datagrid Not Firing

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 1
Reputation: lbob is an unknown quantity at this point 
Solved Threads: 0
lbob lbob is offline Offline
Newbie Poster

Dynamic Buttons in Datagrid Not Firing

 
0
  #1
Apr 24th, 2008
Hi

I've got a datagrid that I'm populating and depending on the data returned depends on what button I add. That all works fine until I click on the button and it posts the page and loses everything. Basically I can't get the button to fire.

Please help!
Lbob
This is my code
  1. Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. If Not Page.IsPostBack Then
  3. bindDG()
  4. End If
  5. End Sub
  6.  
  7. Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
  8. If e.Item.ItemType = ListItemType.Item Then
  9. Dim btn As Button
  10. btn = New Button
  11. btn.Text = "Click Me"
  12. btn.ID = e.Item.Cells(i).Text
  13.  
  14. AddHandler btn.Click, AddressOf btn_Click
  15. e.Item.Cells(i).Controls.Add(btn)
  16. end if
  17. End Sub
  18.  
  19. Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  20. Select Case CType(sender, Button).Text
  21. Case "Click Me"
  22. trace.write("Hello")
  23. End Select
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Dynamic Buttons in Datagrid Not Firing

 
0
  #2
Apr 27th, 2008
because you are dynamically creating buttons they are not retaining state on postback and therefore wont fire the event assigned to them. You need to re-instantiate each dynamic control individually. E.g. when creating each control assign it an ID and save this to the viewstate. Then on page load loop through viewstate to find you controls and rebuild them. Some good examples are here

or you could use the "ItemCommand" event of the datagrid. This will fire if you set a commandname to your buttons when you create them and you wont need to retain state as above (Which can be messy if not implemented properly).
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 291
Reputation: ericstenson is an unknown quantity at this point 
Solved Threads: 29
Team Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

Re: Dynamic Buttons in Datagrid Not Firing

 
0
  #3
Apr 27th, 2008
i would agree with the above, give them a commandname and handle that on pb.
--
"Dummy."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC