Can Someone Please Explain...

Discussion in 'ASP .Net' started by BluDog, Oct 4, 2004.

  1. BluDog

    BluDog Guest

    Hi

    I am trying to test dynamically created controls, to do this i have
    added a placeholder to a WbForm and added the following code behind:

    Private Property Count() As Integer
    Get
    If ViewState("Count") Is Nothing Then ViewState("Count") = 1
    Return CType(ViewState("Count"), Integer)
    End Get
    Set(ByVal Value As Integer)
    ViewState("Count") = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then AddButton()
    End Sub

    Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
    AddButton()
    End Sub

    Private Sub AddButton()

    Dim button As New Button
    button.Text = "Button" & Count.ToString
    button.ID = "Button" & Count.ToString
    AddHandler button.Click, AddressOf button_Click
    PlaceHolder1.Controls.Add(button)
    Count += 1

    End Sub

    What i would expect is a button to be added to the placeholder during
    the first load of the page, then each time the button is cllicked
    another button to be added to the form.

    However the original button is added in the load event, but the click
    event is never handled on the dynamically created buttons, so on
    postbacks no buttons are shown.

    Can anyone explain how to acheive what i am afer here?

    Thanks

    Blu.
     
    BluDog, Oct 4, 2004
    #1
    1. Advertising

  2. BluDog

    Karl Seguin Guest

    When you click the button and postback, the button isn't re-created, thus
    the event is never fired. People make this mistake often. You don't have
    to set the eventhandler when originally creating the item, you need to set
    it on postback.

    If you simply remove the not ispostback check and always call AddButton in
    page_load it'll work.

    To reinforce my original point though, you could actually do:

    Dim button As New Button
    button.Text = "Button" & Count.ToString
    button.ID = "Button" & Count.ToString
    if page.ispostback then
    AddHandler button.Click, AddressOf button_Click
    end if
    PlaceHolder1.Controls.Add(button)
    Count += 1

    notice how the handler is only hooked up on postback - I don't recommend it,
    since it'll only avoid a little overhead but will make it look more
    confusing. I just wanted to point it out to illustrate that events need to
    be hooked on postback, not on non-postback.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "BluDog" <> wrote in message
    news:...
    > Hi
    >
    > I am trying to test dynamically created controls, to do this i have
    > added a placeholder to a WbForm and added the following code behind:
    >
    > Private Property Count() As Integer
    > Get
    > If ViewState("Count") Is Nothing Then ViewState("Count") = 1
    > Return CType(ViewState("Count"), Integer)
    > End Get
    > Set(ByVal Value As Integer)
    > ViewState("Count") = Value
    > End Set
    > End Property
    >
    > Private Sub Page_Load(ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) Handles MyBase.Load
    > If Not IsPostBack Then AddButton()
    > End Sub
    >
    > Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
    > AddButton()
    > End Sub
    >
    > Private Sub AddButton()
    >
    > Dim button As New Button
    > button.Text = "Button" & Count.ToString
    > button.ID = "Button" & Count.ToString
    > AddHandler button.Click, AddressOf button_Click
    > PlaceHolder1.Controls.Add(button)
    > Count += 1
    >
    > End Sub
    >
    > What i would expect is a button to be added to the placeholder during
    > the first load of the page, then each time the button is cllicked
    > another button to be added to the form.
    >
    > However the original button is added in the load event, but the click
    > event is never handled on the dynamically created buttons, so on
    > postbacks no buttons are shown.
    >
    > Can anyone explain how to acheive what i am afer here?
    >
    > Thanks
    >
    > Blu.
     
    Karl Seguin, Oct 4, 2004
    #2
    1. Advertising

  3. BluDog

    BluDog Guest

    >If you simply remove the not ispostback check and always call AddButton in
    >page_load it'll work.


    Karl,

    Thanks for your comments, however this only sorts out the event
    handler for the original button (button1) added in the page load. Any
    buttons added in the click event of the button are not added.

    This is only a test page that i knocked up to try and understand
    ViewState and PostBack on dynamically created controls. As it stands i
    can't see events on dynamically created controls evern being handled
    properly as more and more postbacks take place.

    Cheeers

    Blu.
     
    BluDog, Oct 4, 2004
    #3
  4. BluDog

    Karl Seguin Guest

    Blu,
    I understand where you are coming from. However the same holds true with
    the buttons added in the click...they also need to be readded.

    Denis Bauer has a placeholder control that helps you do this..though I've
    never used it:
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx


    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "BluDog" <> wrote in message
    news:...
    > >If you simply remove the not ispostback check and always call AddButton

    in
    > >page_load it'll work.

    >
    > Karl,
    >
    > Thanks for your comments, however this only sorts out the event
    > handler for the original button (button1) added in the page load. Any
    > buttons added in the click event of the button are not added.
    >
    > This is only a test page that i knocked up to try and understand
    > ViewState and PostBack on dynamically created controls. As it stands i
    > can't see events on dynamically created controls evern being handled
    > properly as more and more postbacks take place.
    >
    > Cheeers
    >
    > Blu.
     
    Karl Seguin, Oct 4, 2004
    #4
  5. BluDog

    BluDog Guest

    On Mon, 4 Oct 2004 14:17:11 -0400, "Karl Seguin" <karl REMOVE @ REMOVE
    openmymind REMOVEMETOO . ANDME net> wrote:

    >Blu,
    >I understand where you are coming from. However the same holds true with
    >the buttons added in the click...they also need to be readded.
    >
    >Denis Bauer has a placeholder control that helps you do this..though I've
    >never used it:
    >http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    >
    >
    >Karl


    Thanks Karl, you have pretty much confirmed my conclusions on this -
    every postback needs to be aware of previous postbacks to ensure
    anything done dynamically previously can be repeated. Thus making an
    application in a webpage a little difficult.

    Interesting control though, gunna have a good play today.
     
    BluDog, Oct 5, 2004
    #5
    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. suzy
    Replies:
    3
    Views:
    450
  2. Mike Henley

    can someone explain this to me please

    Mike Henley, Sep 6, 2003, in forum: Java
    Replies:
    6
    Views:
    380
    Mike Henley
    Sep 7, 2003
  3. Replies:
    4
    Views:
    895
    Jukka K. Korpela
    Dec 4, 2003
  4. harry
    Replies:
    12
    Views:
    743
    Blinky the Shark
    May 26, 2005
  5. Replies:
    5
    Views:
    333
    dorayme
    Oct 13, 2006
Loading...

Share This Page