Howdy, Stranger!

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


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.