Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Added Control & Viewstate: Concept ot Bug?

Reply
Thread Tools

Added Control & Viewstate: Concept ot Bug?

 
 
Dominic
Guest
Posts: n/a
 
      12-03-2003
I was off my rocker on this one. I've finally figured out the
behavior with the most simple test case but can't come up with reason
to support it. Here is the scenario:

1. Add a textbox programatically
2. If it is the first page_load (i.e. not postback) set the initial
value (.text) and set visible to false
3. On postback make the textbox visible. Guess what? The value is
gone!

Here is the setup for the basic test case: a form with a button to
postback and a placeholder (PlaceHolder1) for the textbox control.
Here is the Page_load code:

Dim tbToAdd As TextBox = New TextBox
tbToAdd.ID = "TextBox1"
If Not IsPostBack Then PlaceHolder1.Visible = False
If IsPostBack Then PlaceHolder1.Visible = True
If Not IsPostBack Then tbToAdd.Text = "Hello World"
PlaceHolder1.Controls.Add(tbToAdd)

This testcase yields a blank textbox on postback. What I found was
that if I take the second to last line where the .text attribute is
set if not postback, and put it AFTER the control is added, it then
works. To further confuse me, it seems not to be the case with a
dropdownlist whereas I can set the .Selected attribute of a list item
BEFORE the dropdownlist control is added under the same scenario and
it is preserved on postback/visible. Can anybody make sense of this,
then please explain it to me?

Thanks, Dominic.
 
Reply With Quote
 
 
 
 
Lostinet.Web Support
Guest
Posts: n/a
 
      12-08-2003
If Not IsPostBack Then tbToAdd.Text = "Hello World"
PlaceHolder1.Controls.Add(tbToAdd)

should change the order:

PlaceHolder1.Controls.Add(tbToAdd)
If Not IsPostBack Then tbToAdd.Text = "Hello World"

if you se the Text before add it to parent's Controls collection,
the value is as 'initialization value' and the value will not persist in
ViewState.


"Dominic" <(E-Mail Removed)> ????
news:(E-Mail Removed) om...
> I was off my rocker on this one. I've finally figured out the
> behavior with the most simple test case but can't come up with reason
> to support it. Here is the scenario:
>
> 1. Add a textbox programatically
> 2. If it is the first page_load (i.e. not postback) set the initial
> value (.text) and set visible to false
> 3. On postback make the textbox visible. Guess what? The value is
> gone!
>
> Here is the setup for the basic test case: a form with a button to
> postback and a placeholder (PlaceHolder1) for the textbox control.
> Here is the Page_load code:
>
> Dim tbToAdd As TextBox = New TextBox
> tbToAdd.ID = "TextBox1"
> If Not IsPostBack Then PlaceHolder1.Visible = False
> If IsPostBack Then PlaceHolder1.Visible = True
> If Not IsPostBack Then tbToAdd.Text = "Hello World"
> PlaceHolder1.Controls.Add(tbToAdd)
>
> This testcase yields a blank textbox on postback. What I found was
> that if I take the second to last line where the .text attribute is
> set if not postback, and put it AFTER the control is added, it then
> works. To further confuse me, it seems not to be the case with a
> dropdownlist whereas I can set the .Selected attribute of a list item
> BEFORE the dropdownlist control is added under the same scenario and
> it is preserved on postback/visible. Can anybody make sense of this,
> then please explain it to me?
>
> Thanks, Dominic.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView control and a dynamically added DropDownList control! ata@mailinator.com ASP .Net 3 06-06-2008 02:49 PM
Solution for posterity: GridView, Datakeys, and "Item has already been added. Key in dictionary: 'CategoryID' Key being added: 'CategoryID'" ASP .Net 2 11-02-2006 04:48 AM
Responding to Button control events when Button is added to a Table Control Jim Gilligan ASP .Net Web Controls 0 09-01-2005 06:29 PM
reference web control in dynamically added user control, ASP.NET 2 =?Utf-8?B?U2FtdWVs?= ASP .Net 7 08-10-2005 02:12 AM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM



Advertisments