Results 1 to 6 of 6

Thread: [resolved] Dynamic controls disappear on click

Threaded View

  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


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