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
[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
Comment