Help getting onClick to work with dynamic added control.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcelary
    New Member
    • Jul 2008
    • 3

    Help getting onClick to work with dynamic added control.

    When the user clicks a radio button it creates a postback where I add a button control dynamically. My problem is I cannot get the dynamically added button to work with on an click event. The button was created in code behind page.

    [code=vbnet]Public Sub rosterAddContro ls()
    Dim myBtn As New Button()
    myBtn.Style.Ite m("z-index") = "100"
    myBtn.Style.Ite m("position") = "absolute"
    myBtn.Style.Ite m("top") = (x - 30) & "px"
    myBtn.Style.Ite m("left") = y & "px"
    myBtn.ID = "myBtn"
    myBtn.Text = "Button 8974"
    myBtn.OnClientC lick = "myBtn_Clic k()"
    PlaceHolder1.Co ntrols.Add(myBt n)
    End Sub[/code]

    This above code works fine and the button myBtn is displayed when above method is called.
    From the help forms that I have read, I gathered I needed to make an AddHandler statement in the Page_Init as shown below.

    [code=vbnet]Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Init
    AddHandler myBtn.Click, AddressOf myBtn_Click
    End Sub[/code]

    Lastly, I have created a method that should be called when myBtn is clicked. But the below method is never called.

    [code=vbnet]Protected Sub myBtn_Click(ByV al sender As Object, ByVal e As System.EventArg s)
    Dim test As String
    test = "this is a test "
    End Sub[/code]

    Can someone please help me get the dynamically added button myBtn to fire a onClick event.

    Thanks
    lance
    Last edited by Frinavale; Jul 1 '08, 02:59 PM. Reason: added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The button has a scope that only exists in this sub. The button is added to the page but that's it....
    [code=vbnet]
    Public Sub rosterAddContro ls()
    Dim myBtn As New Button()
    myBtn.Style.Ite m("z-index") = "100"
    myBtn.Style.Ite m("position") = "absolute"
    myBtn.Style.Ite m("top") = (x - 30) & "px"
    myBtn.Style.Ite m("left") = y & "px"
    myBtn.ID = "myBtn"
    myBtn.Text = "Button 8974"
    myBtn.OnClientC lick = "myBtn_Clic k()"
    PlaceHolder1.Co ntrols.Add(myBt n)
    End Sub[/Code]

    What you need to do is declare the button outside of this sub. Also, you need to declare it "WithEvents " or else no events will be fired...

    eg:


    Code:
    Private WithEvents myBtn As Button
    
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        rosterAddControls()
    End Sub
    
    Public Sub rosterAddControls()
        myBtn = New Button
        myBtn.Style.Item("z-index") = "100"
        myBtn.Style.Item("position") = "absolute"
        myBtn.Style.Item("top") = (x - 30) & "px"
        myBtn.Style.Item("left") = y & "px"
        myBtn.ID = "myBtn"
        myBtn.Text = "Button 8974"
        PlaceHolder1.Controls.Add(myBtn)
    End Sub
    Now your button click event will fire:

    [code=vbnet]Protected Sub myBtn_Click(ByV al sender As Object, ByVal e As System.EventArg s) Handles myBtn.Click
    Dim test As String
    test = "this is a test "
    End Sub[/code]



    Cheers!

    -Frinny

    Comment

    • mcelary
      New Member
      • Jul 2008
      • 3

      #3
      Frinavale thanks for your help.
      I really appreciate the time you took and the corrected code you shared. I have been trying to solve this problem for about a week.

      Thanks for the excellent help.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by mcelary
        Frinavale thanks for your help.
        I really appreciate the time you took and the corrected code you shared. I have been trying to solve this problem for about a week.

        Thanks for the excellent help.
        Glad to have helped!

        Cheers!

        Comment

        • mcelary
          New Member
          • Jul 2008
          • 3

          #5
          Again thanks to Frinavale for help on this.
          The problem now is being able to save the value of a textbox into a variable I dimed as string "oracleID"
          I have added the following code in the Public Sub rosterAddContro ls()
          txtEditOracle.S tyle.Item("z-index") = "100"
          txtEditOracle.S tyle.Item("posi tion") = "absolute"
          txtEditOracle.S tyle.Item("Font-Size") = fntSize & "px"
          txtEditOracle.S tyle.Item("top" ) = x & "px"
          txtEditOracle.S tyle.Item("left ") = y & "px"
          txtEditOracle.V isible = showRosterTools
          txtEditOracle.W idth = 40

          oracleID = txtOracleID.Tex t.ToString

          Problem is when this sub is called ...
          Protected Sub myBtn_Click(ByV al sender As Object, ByVal e As System.EventArg s) Handles myBtn.Click
          addToDatabase()
          End Sub

          ... the postback calls the page init
          Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Init
          rosterAddContro ls()
          End Sub

          ... this recreates the textbox with no value. If I do not call "rosterAddContr ols()" then the textbox control is not even created.

          any help on this would be appreciated.
          thanks
          lance

          Comment

          Working...