Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris Larson 48 posts 63 karma points
    Oct 23, 2009 @ 05:38
    Chris Larson
    0

    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]:

    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

     

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Oct 23, 2009 @ 09:27
    Richard Soeteman
    0

    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 :)

    Protected

     

    Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

     

    Dim _button As New Button

    _button.ID =

    "SubmitButtonID"

    _button.Text =

    "Submit"

    Controls.Add(_button)

     

    AddHandler _button.Click, AddressOf CustomEventHandler

     

    End Sub

     

    Public Sub CustomEventHandler(ByVal s As Object, ByVal e As EventArgs)

     

    ' do some event handling here

     

    End Sub

     

    Hope it helps you,

    Richard

  • Chris Larson 48 posts 63 karma points
    Oct 23, 2009 @ 23:33
    Chris Larson
    0

    I knew I was missing something. Page_Init makes a huge difference.

    Tks Richard.

Please Sign in or register to post replies

Write your reply to:

Draft