Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How a PlaceHolder control can resist a postback?

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
Hi,

I generate a textbox server control dynamically through a subroutine when a user select an specific option from a dropdownlist. I do it thus:
Code:
Sub Activ(sender As Object, e As System.EventArgs)
   Dim activ As Integer
   activ = which_Activ.SelectedItem.Value
   
   If (activ = 7) Then
  
   Dim myBox As TextBox = New TextBox()
   otherActiv.Controls.Add(myBox)
    
   End If

End Sub 


<html>
<body>
<form>
<asp:PlaceHolder ID="otherActiv" runat="server"/>
...
But the problem is after the user has generated the textbox placed in ‘PlaceHolder’, if some more postback occurs the texbox is erased. How can I avoid this so that the textbox generated persist like the other web form server controls?


Thanks,
Cesar
 
You need to make sure to generate the control everytime the page posts back.
Remember that web pages are actually stateless. We've just made some really fancy work arounds so that they seem to have a persistant state.
When the page posts back it actually redraws the entire page by parsing the aspx file before sending the content back to the browser. Since your control doesn't exist on the aspx file it isn't redrawn. So to reiterate you must dynamically create the control everytime the page posts back when it's required. A second option is to have the text box currently on the page at design time and to hide/show it when you need to.

The text box's values are persisted over a post back through the use of the viewstate which is why they can keep their .Text or .Visible values over a post back.

Hope that helped

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Ok, but I have a 'message.text' that is generated as well with the same subroutine and it persists after postback.

I have tried to generate the 'textbox' dynamically every time the page loads thus:(and it does the same)
Code:
Sub Page_Load()
   Dim activ As Integer
   activ = which_Activ.SelectedItem.Value
   
   If (activ = 7) Then
     Dim myBox As TextBox = New TextBox()
     otherActiv.Controls.Add(myBox)
    
   End If
End Sub
Which is exactly the second option to have the text box currently on the page at design time and to hide/show it when you need to ? Do you think that is a better solution to my purpose?

Thank you vm,
Cesar



 
Err if your using VB i think you need a Handles MyBase.Load after the Page_Load. Not sure I haven't used VB in a long time.

The other option I was talking about is to put the text box on the page at design time rather than in your code. Set the .Visible Property to False and in your original code
Code:
Sub Activ(sender As Object, e As System.EventArgs)
   Dim activ As Integer
   activ = which_Activ.SelectedItem.Value
   
   If (activ = 7) Then  
     txtBox.Visible = True
   End If

End Sub

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thank you, I am going to check your answer in more detail later.

Thanks again,
Cesar
 
Ok, Now I have used the second option, the text box on the page at design time with the Visible property set to false, and when a user select the dropdownlist option 7 a subroutine is triggered in order to change the property to true. And this time the textbox resists the postback.
The code:
Code:
Sub Activ(sender As Object, e As System.EventArgs)
   Dim activ As Integer
   activ = which_Activ.SelectedItem.Value
   
   If (activ = 7) Then  
     txtBox.Visible = True
     message1.Text = "Write your activity"
   End If

End Sub 

<html>
<body>
<form>
<asp:textbox ID="activDif" runat="server" Visible="false" />
<asp:label ID="message1" runat="server" />


Anyway I don' t understand why now the text box persists visible after another postback, since when the page is postback the Subroutine isn't executed, so the page execute the text box control with the visible property set to false.
Why using 'PlaceHolder' the text box disappears after another postback and not now with this second system?
 
As I mentioned above
The text box's values are persisted over a post back through the use of the viewstate.
This is how they can keep their .Text or .Visible values over a post back.

The page is built with the box.Visible set to False but when the ViewState values are propegated the box.Visible property is set to True.

ViewState stores only properties and such NOT actual controls



That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top