RSS Forums RSS
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 899 | Replies: 2
Reply
Join Date: Apr 2008
Posts: 1
Reputation: lbob is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lbob lbob is offline Offline
Newbie Poster

Dynamic Buttons in Datagrid Not Firing

  #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
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
            bindDG()
End If
End Sub

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
If e.Item.ItemType = ListItemType.Item  Then
Dim btn As Button
btn = New Button
btn.Text = "Click Me"
btn.ID = e.Item.Cells(i).Text

AddHandler btn.Click, AddressOf btn_Click
e.Item.Cells(i).Controls.Add(btn)
end if
End Sub

Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Select Case CType(sender, Button).Text
Case "Click Me"
trace.write("Hello")
End Select
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 379
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Dynamic Buttons in Datagrid Not Firing

  #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  
Join Date: Dec 2007
Posts: 287
Reputation: ericstenson is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 28
Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

Re: Dynamic Buttons in Datagrid Not Firing

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

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:14 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC