Does anyone have experience with adding a dynamic button and attaching an event handler to that button in Umbraco? I have tried a couple different methods for creating buttons dynamically and get the buttons to render properly, but I'm having problems getting an event handler attached to them. Any suggestions would be welcome.
Sample Code [vb]:
Public Function RenderButton() As String Dim _button as New Button _button.Text = "Submit" AddHandler _button.Click, AddressOf CustomEventHandler Dim _TextWriter as New System.IO.StringWriter Dim _Renderer as New HtmlTextWriter = New HtmlTextWriter(_TextWriter) _button.RenderControl(_Renderer) Return _TextWriter End Function Public Sub CustomEventHandler(ByVal s as Object, ByVal e as EventArgs) ' do some event handling here End Sub
1. When you create a control (in your case a button) you need to do that in the Page_Init event. 2, A dynamic control must have an ID 3. The control must exist on the controltree.
I would Modify the code below you find an example. Can't test it so the compiler might complain :)
Protected
Sub Page_Init(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Init
Dynamic Buttons and Event Handlers
Does anyone have experience with adding a dynamic button and attaching an event handler to that button in Umbraco? I have tried a couple different methods for creating buttons dynamically and get the buttons to render properly, but I'm having problems getting an event handler attached to them. Any suggestions would be welcome.
Sample Code [vb]:
Hi Chris,
I see a few things.
1. When you create a control (in your case a button) you need to do that in the Page_Init event.
2, A dynamic control must have an ID
3. The control must exist on the controltree.
I would Modify the code below you find an example. Can't test it so the compiler might complain :)
Hope it helps you,
Richard
I knew I was missing something. Page_Init makes a huge difference.
Tks Richard.
is working on a reply...