dynamic created textbox disappear when button is clicked

georgepatotk

Contributor
Joined
Mar 1, 2004
Messages
432
Location
Malaysia
Code:
    Sub ShowMyTextBox()
        Dim MyTextBox As New TextBox
        MyTextBox.ID = "MyTextBox" 
        MyTextBox.Text = "MyTextBox"
        Me.Panel1.Controls.Add(MyTextBox)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowMyTextBox()
    End Sub

As pasted above, I have two button in a web form.
Button1 is to create a textbox.
I have the other button named Button2.
When I press Button2, the created text box is disappear.
Can you tell me why the created textbox is disappear when I press button2??
 
YeH...
thanks a lot.. I think I had figured out a way to do so..

First, I declare a global flag variable in my main aspx.

When I create ShowMyTextBox(), I will set the flag to true.

so, everytime I post back the page, I will read the global variable. If it is not false, I will call ShowMyTextBox()
 
So it worked......good. But I have a question for you, when you recreate the control, are you able to read the values entered by the user in this textbox??
 
ok, a way to make the value remained.

Code:
Dim txt1 as new TextBox

Private Sub Load(....)
     if txt1.text <> "" then
          me.controls.add(txt1)
     end if
End Sub

Private Sub Button1_Click(.....) Handles Button1.Click
     txt1.text = "Hello"
     me.controls.add(txt1)
End Sub

Got my idea???
 
Back
Top