Event never fired


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Event never fired

  1. #1
    ip266 Guest

    Event never fired


    Please help for the following senario:
    I'm creating a button programmatically. Why if I use IsPostBack, the MyButton_Click
    function never be executed. What's wrong? Below is the code:

    Private Sub Page_Load(Sender as object, e as EventArgs)
    If Not IsPostBack Then
    AddButtonToForm()
    End If
    End Sub

    Sub AddButtonToForm()
    Dim objBtn As New Button()
    ...
    AddHander objBtn.Click, AddressOf MyButton_Click
    MyForm.Controls.Add(objBtn)
    End Sub

    Sub MyButton_Click(Sender as Object, e As EventArgs)
    Response.Write ("Clicked")
    End Sub



  2. #2
    Constance J. Petersen Guest

    Re: Event never fired

    "ip266" <ip266@yahoo.com> wrote in message news:3d660210$1@10.1.10.29...
    >
    > Please help for the following senario:
    > I'm creating a button programmatically. Why if I use IsPostBack, the

    MyButton_Click
    > function never be executed. What's wrong? Below is the code:


    Have you added the control to a container such as a placeholder or panel?

    Are you following the advice on this documentation page?

    ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbtskaddingcontrolstowebformspageprogramm
    atically.htm

    --
    Constance Petersen, DevX newsgroup section leader
    New look; new content: http://www.smartisans.com/
    Hot off the press!
    "Programming the Web with Visual Basic .NET"
    http://amazon.com/exec/obidos/ASIN/1...tancepeterseA/
    --
    Please reply in the newsgroup so everyone can benefit



  3. #3
    ip266 Guest

    Re: Event never fired


    Thanks for your help. But I found that article has not touching about my issue.
    You mention for container like placeholder or panel. In fact, htmlform itself
    is a container too, right? Base on my sample code, can you tell me what's
    wrong with it? Hope to hear from you soon. Thanks a lot!!

    "Constance J. Petersen" <constance@smartisans.com> wrote:
    >"ip266" <ip266@yahoo.com> wrote in message news:3d660210$1@10.1.10.29...
    >>
    >> Please help for the following senario:
    >> I'm creating a button programmatically. Why if I use IsPostBack, the

    >MyButton_Click
    >> function never be executed. What's wrong? Below is the code:

    >
    >Have you added the control to a container such as a placeholder or panel?
    >
    >Are you following the advice on this documentation page?
    >
    >ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbtskaddingcontrolstowebformspageprogramm
    >atically.htm
    >
    >--
    >Constance Petersen, DevX newsgroup section leader
    >New look; new content: http://www.smartisans.com/
    >Hot off the press!
    >"Programming the Web with Visual Basic .NET"
    >http://amazon.com/exec/obidos/ASIN/1...tancepeterseA/
    >--
    >Please reply in the newsgroup so everyone can benefit
    >
    >



  4. #4
    Constance J. Petersen Guest

    Re: Event never fired

    "ip266" <ip266@yahoo.com> wrote in message news:3d699221$1@10.1.10.29...
    > Thanks for your help. But I found that article has not touching about my

    issue.
    > You mention for container like placeholder or panel. In fact, htmlform itself
    > is a container too, right? Base on my sample code, can you tell me what's
    > wrong with it? Hope to hear from you soon. Thanks a lot!!


    Just add the button control every time (remove the If Not IsPostBack). Here's an
    example. To try it out, drop a label and a placeholder on the form:

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim myButton As Button = New Button()
    myButton.Text = "click me"
    AddHandler myButton.Click, AddressOf myButton_Click
    PlaceHolder1.Controls.Add(myButton)
    End Sub


    Sub myButton_Click(ByVal Sender As Object, ByVal e As EventArgs)
    If Label1.Text = "" Then
    Label1.Text = "You clicked me."
    Else
    Label1.Text = Label1.Text & " Again."
    End If
    End Sub

    --
    Constance Petersen, DevX newsgroup section leader
    New look; new content: http://www.smartisans.com/
    Hot off the press!
    "Programming the Web with Visual Basic .NET"
    http://amazon.com/exec/obidos/ASIN/1...tancepeterseA/
    --
    Please reply in the newsgroup so everyone can benefit



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center