Button created with dim new button click event is not working

G

gce

Allthough button2 created with the designer works 100%. The button I created
(inside the table) with dim button1 as new button doesn't give me a click
event. Please help.

Public Class WebForm8

Inherits System.Web.UI.Page


#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Table1 As System.Web.UI.WebControls.Table
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim rij1 As New TableRow
Dim cel1 As New TableCell
Dim cel2 As New TableCell

Dim header As New TableHeaderCell
header.RowSpan = 1
header.ColumnSpan = 2
header.Text = "test"
header.Font.Bold = True
header.HorizontalAlign = HorizontalAlign.Center
header.VerticalAlign = VerticalAlign.Middle

Dim headerrow As New TableRow
headerrow.Cells.Add(header)

Table1.Rows.Add(headerrow)

Dim button1 As New Button

button1.Text = "knop"

cel1.Controls.Add(button1)
cel2.Text = "cel2"
rij1.Cells.Add(cel1)
rij1.Cells.Add(cel2)

Table1.Rows.Add(rij1)




End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim popupScript As String = "<script language='javascript'>" & _
"window.open('http://www.nu.nl', 'NU', " & _
"'width=200, height=200, menubar=yes, resizable=no')" &
_
"</script>"

Page.RegisterStartupScript("PopupScript", popupScript)
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim popupScript As String = "<script language='javascript'>" & _
"window.open('http://www.nu.nl', 'NU', " & _
"'width=200, height=200, menubar=yes,
resizable=no')" & _
"</script>"

Page.RegisterStartupScript("PopupScript", popupScript)
End Sub
End Class
 
M

Marina

That's because by Page_Load, button1 is already a valid button object, and
its event handler is hooked up.

You are discarding that object and creating a new one. But you are not
reattaching the event handler, which you must do via AddHandler. The
declaritive way of specifying the handler assumes you are letting the
asp.net runtime take care of instantiating the object, and not throwing away
what it does to do it yourself.
 
G

gce

Super ! That works great.

But now I want the same thing for a radiobutton and it doesn't work. My hope
was that when I check the radiobutton, the radiobutton1_checkedchanged would
be fired. Nope.

Dim radiobutton1 As New RadioButton
AddHandler radiobutton1.CheckedChanged, AddressOf
radiobutton1_CheckedChanged

Private Sub radiobutton1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles radiobutton1.CheckedChanged
Dim popupScript As String = "<script language='javascript'>" & _
"window.open('http://www.nu.nl', 'NU', " & _
"'width=200, height=200, menubar=yes,
resizable=no')" & _
"</script>"

Page.RegisterStartupScript("PopupScript", popupScript)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top