Asked by:
dynamic controls losing event handlers

Question
-
User1858287507 posted
hi there, weird problem here havent done much work with mobile development but i have done loads of standard we form work. All im trying to is display a dynamic list of butttons (each representing a product), im adding an onlcik event handler to them so i can redirect to show more details
heres the code
Private mobjCard As FutureStep.DeliveredForYouBusinessClasses.Card
Private Sub frmCardList_Activate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmCardList.Activate
Dim objCArd As FutureStep.DeliveredForYouBusinessClasses.Card
Dim objImage As MobileControls.Image
Dim objButton As MobileControls.Command
If Not mobjCategory Is Nothing Then
For Each objCArd In mobjCategory.CardCollection
objButton = New MobileControls.Command
objButton.Alignment = MobileControls.Alignment.Center
objButton.Text = objCArd.Title
objButton.ID = "btn" & objCArd.ID
AddHandler objButton.Click, AddressOf LoadCard
objButton.ImageUrl = "/DeliveredForYou/DeliverACard/ImageServe.aspx?id=" & objCArd.ID & "&ImageTypeId=1&Thumb=1"
Me.pnlCardlist.Controls.Add(objButton)
Next
End If
End Sub
Private Sub LoadCard(ByVal sender As Object, ByVal e As System.EventArgs)
mobjCard = New FutureStep.DeliveredForYouBusinessClasses.Card(sender.id.replace("btn", ""))
UpdatePageObjects()
ActiveForm = frmCard
End Sub
The buttons load fine at first but when one is clicked 2 things fail to happen
1. the loadcard method is not run
2. frmCardList_Activate is not run!!! only Page_Load is.
i assume that .NET cant figure out which form the event came from (it needs to reloaded to rebuild the controls and reattach the events)
If this doesnt work then it effectively mean .NET mobile doesnt support dymanic controls
Tell me its not true!!
Many Thanks
AshleySunday, February 19, 2006 9:41 AM
All replies
-
User1858287507 posted
my suspicions were correct, i was able to reload the dynamic controls using a session var. As you can see its is a bit of a monstrousity, coded by satan himself [;)]
Private mobjCard As FutureStep.DeliveredForYouBusinessClasses.Card
sub Page_Load()
if not session("Rediculos") is nothing then
frmCardList_Activate(me, nothing)
end if
end sub
Private Sub frmCardList_Activate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmCardList.Activate
Dim objCArd As FutureStep.DeliveredForYouBusinessClasses.Card
Dim objImage As MobileControls.Image
Dim objButton As MobileControls.Command
If Not mobjCategory Is Nothing Then
For Each objCArd In mobjCategory.CardCollection
objButton = New MobileControls.Command
objButton.Alignment = MobileControls.Alignment.Center
objButton.Text = objCArd.Title
objButton.ID = "btn" & objCArd.ID
AddHandler objButton.Click, AddressOf LoadCard
objButton.ImageUrl = "/DeliveredForYou/DeliverACard/ImageServe.aspx?id=" & objCArd.ID & "&ImageTypeId=1&Thumb=1"
Me.pnlCardlist.Controls.Add(objButton)
Nextsession("Rediculos") = true
End If
End Sub
Private Sub LoadCard(ByVal sender As Object, ByVal e As System.EventArgs)
mobjCard = New FutureStep.DeliveredForYouBusinessClasses.Card(sender.id.replace("btn", ""))session.remove("Rediculos")
UpdatePageObjects()
ActiveForm = frmCard
End Sub
anyone know of a better way to this?
Monday, February 20, 2006 11:06 AM -
User-1113234820 posted
Mostly, you need to add the EventHandler in the Page_Load() , not in the Form_Active(), becuase, let's imagine, when you click the button, it posts back to the page, Page_Load() gets called, then it trys to find the handler, but at that point, Form_Active is not called, you know each page is a totally new page, so the event handler is not hooked up at all in the post back process.
So, what you need to do is: in the Page_Load(), add:
if (Page.IsPostback)
{
// Create all the buttons again, and add handlers,}
This is why it works when you put the Rediculos in Session, becuase you called the Form Active function again.Wednesday, February 22, 2006 9:39 AM