Help getting onClick to work with dynamic added control. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help getting onClick to work with dynamic added control.

lance_mlance_m Posts: 2Member
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.

[color=Blue]Public Sub rosterAddControls()
Dim myBtn As 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"
myBtn.OnClientClick = "myBtn_Click()"
End Sub[/color]

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.

[color=Blue] Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
AddHandler myBtn.Click, AddressOf myBtn_Click
End Sub[/color]

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

[color=Blue] Protected Sub myBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim test As String
test = "this is a test "
End Sub[/color]

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



Sign In or Register to comment.