Button created with dim new button click event is not working

Discussion in 'ASP .Net' started by gce, Apr 11, 2005.

  1. gce

    gce Guest

    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
    gce, Apr 11, 2005
    #1
    1. Advertising

  2. gce

    Marina Guest

    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.

    "gce" <> wrote in message
    news:d3ecfn$lc8$1.ov.home.nl...
    > 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
    >
    >
    Marina, Apr 11, 2005
    #2
    1. Advertising

  3. gce

    gce Guest

    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
    gce, Apr 11, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    815
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    953
  3. Denis Palas
    Replies:
    1
    Views:
    491
  4. Max2006
    Replies:
    2
    Views:
    1,549
    Walter Wang [MSFT]
    Sep 19, 2007
  5. Florian Kaufmann
    Replies:
    1
    Views:
    367
Loading...

Share This Page