I've reviewed many of the previous posts on this to make sure I'm not doing something wrong but I'm stumped. I am dynamically creating LinkButtons and I'm assigning a generic Click event to them. The event doesn't seem to be firing. I have the code set
in the Page_Init.
My code:
For Each CalendarDay As TableCell In CalendarRow.Cells
Dim DateLink As New LinkButton
The link button should be recreated each time along with the event handler for each postback. By dynamically creating a link button, you take the responsibility of adding the link button to the control tree of the page. The above code should be placed in
Page_PreInit() preferrably or atleast in Page_Load() for each postback.
Update: When the program builds, I'm getting a number of first chance exception errors although the program executres properly (without the click event, obviously)
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
JonnyBravoII
Member
43 Points
74 Posts
AddHandler for click event
Apr 30, 2012 09:55 AM|LINK
I've reviewed many of the previous posts on this to make sure I'm not doing something wrong but I'm stumped. I am dynamically creating LinkButtons and I'm assigning a generic Click event to them. The event doesn't seem to be firing. I have the code set in the Page_Init.
My code:
For Each CalendarDay As TableCell In CalendarRow.Cells
Dim DateLink As New LinkButton
Try
AddHandler DateLink.Click, AddressOf dateButton_Click
.......some other code.....
For the dateButton_Click, I just put in a msgbox to see if I'm getting there.
Protected Sub dateButton_Click(sender As Object, e As EventArgs)
MsgBox(sender.ToString)
MsgBox(e.ToString)
End Sub
The event just doesn't fire. Ideas?
vijayst
All-Star
16558 Points
3216 Posts
Microsoft
Re: AddHandler for click event
Apr 30, 2012 10:41 AM|LINK
The link button should be recreated each time along with the event handler for each postback. By dynamically creating a link button, you take the responsibility of adding the link button to the control tree of the page. The above code should be placed in Page_PreInit() preferrably or atleast in Page_Load() for each postback.
http://liteblog.codeplex.com
JonnyBravoII
Member
43 Points
74 Posts
Re: AddHandler for click event
Apr 30, 2012 10:52 AM|LINK
As noted, the code is in Page_Init. I don't think it's possible to put it into PreInit as no page objects will exist.
JonnyBravoII
Member
43 Points
74 Posts
Re: AddHandler for click event
Apr 30, 2012 11:03 AM|LINK
Update: When the program builds, I'm getting a number of first chance exception errors although the program executres properly (without the click event, obviously)
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: AddHandler for click event
May 02, 2012 03:48 AM|LINK
Try this:
Dim DateLink As New LinkButton()
DateLink.Click += New EventHandler(dateButton_Click)
Feedback to us
Develop and promote your apps in Windows Store