Results 1 to 6 of 6

Thread: [resolved] Dynamic controls disappear on click

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question [resolved] Dynamic controls disappear on click

    This is probably a simple solution, but i can't figure it out......

    I dynamically create a control on the page, but it disappears when a button on the page is clicked. Here's an example of what i have:

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'If Me.IsPostBack = True Then Exit Sub
    3.     End Sub
    4.  
    5.     Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    6.         Dim chkSample As New CheckBox
    7.         With chkSample
    8.             .Text = "I was added in the code"
    9.             .ID = "chkSample"
    10.             .EnableViewState = True
    11.         End With
    12.  
    13.         pnlSample.Controls.Clear()
    14.         pnlSample.Controls.Add(chkSample)
    15.         pnlSample.EnableViewState = True
    16.  
    17.         lblTalk.Text = "button ONE was clicked"
    18.     End Sub
    19.  
    20.     Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    21.         lblTalk.Text = "button TWO was clicked"
    22.     End Sub

    I thought that the controls and their state were supposed to remain, but they don't. Run the page and click the first button - the code runs and creates the checkbox. Then click the second button - the checkbox vanishes.

    What am i doing wrong?
    Last edited by MrGTI; Aug 10th, 2005 at 02:23 PM. Reason: resolved
    ~Peter


  2. #2
    Member basilisk's Avatar
    Join Date
    Jan 2002
    Posts
    32

    Re: Dynamic controls disappear on click

    Dynamically added controls need to be readded at every postback. This article explains the ins and outs:
    http://msdn.microsoft.com/library/de.../viewstate.asp

    (there is a section in it about dynamically added controls)

    The following is on Denis Bauer site as he has created a custom control that recreates dynamic controls on postbacks:
    http://www.denisbauer.com/ASPNETCont...aceholder.aspx

    i hope this helps

  3. #3
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Re: Dynamic controls disappear on click

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Me.IsPostBack Then
    call btnOne_Click
    end if
    End Sub

    Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    Dim chkSample As New CheckBox
    With chkSample
    .Text = "I was added in the code"
    .ID = "chkSample"
    .EnableViewState = True
    End With

    pnlSample.Controls.Clear()
    pnlSample.Controls.Add(chkSample)
    pnlSample.EnableViewState = True

    lblTalk.Text = "button ONE was clicked"
    End Sub

    Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    lblTalk.Text = "button TWO was clicked"
    End Sub

    If a post has helped you then Please Rate it!

  4. #4
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Re: Dynamic controls disappear on click

    well that one wont work..u should initialize every object every postback i guess

    If a post has helped you then Please Rate it!

  5. #5

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool

    Thanks guys.

    I read that huge article basilisk. I'm very disappointed that ASP.NET does not retain the dynamically added controls. It looks like a very big oversight on their part.

    I will try out that control by Denis Bauer. It certainly looks like it will do the job. I just wish it wasn't needed.
    ~Peter


  6. #6

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up .

    The control created by Denis Bauer (DynamicControlsPlaceholder) worked great.
    ~Peter


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width