textbox's state does not persist if onTextChanged not defined

H

Henri

Very strange problem :

if I write:

<asp:TextBox runat="server" id="myBox" />

the control's ViewState stays always empty, so it loses its properties if
it's not always displayed.

But if I write:

<asp:TextBox runat="server" id="myBox"
onTextChanged="myBox_Click" />

the control's ViewState is filled, and it keeps its properties even when not
displayed.

Can you try this too?
It's very strange. Other controls don't seem to beheave like this.
Thanks.

Henri
 
F

Fred Newton

What are you asking?

Do you want ViewState? If so, have you tried adding EnableViewState="True"?
as in:
<asp:TextBox id="myBox" runat="server" EnableViewState="True"></asp:TextBox>

Do you Not want ViewState info to be retained? or are you just trying to see
if other people can confirm that your incomplete syntax yields results that
you do not expect?
 
H

Henri

I want ViewState to work.
I've checked the EnableViewState property: it's True.
But ViewState stays empty unless I define OnTextChanged="..."
I wanted to know if some other people have the same problem and if they have
a solution.
 
F

Fred Newton

Okay, a couple of things.
1. For future reference, we can provide you with more accurate assistance if
you provide the actual code you are using - at least as much as is relevant
to the question. The syntax you gave us in your original post (<asp:TextBox
runat="server" id="myBox" />) does not include EnableViewState="True"... so
that doesn't square up with you now saying that you do have
EnableViewState="True".

2. If your TextBox appears within an <asp:panel> that, itself has
EnableViewState="False", then you might get some unexpected behavior... The
panel has no ViewState, but there is an expectation that objects within it
will have ViewState... I haven't confirmed this but recently saw similarly
unexpected behavior with a DropDownList in a Panel that had
EnableViewState="False"

In any case, we're guessing here because we don't have your actual code.

Good Luck
 
H

Henri

Thanks for your help Fred,
but it seems that this behavior is by design.
Even with ViewState enabled, input fields' Value is never stored in it
unless you want to know when Value changed (by defining the OnTextChanged
attribute).
Seems that I'll have to customize TextBox!
 
C

Craig Deelsnyder

Thanks for your help Fred,
but it seems that this behavior is by design.
Even with ViewState enabled, input fields' Value is never stored in it
unless you want to know when Value changed (by defining the OnTextChanged
attribute).
Seems that I'll have to customize TextBox!

This is because ViewState only stores what .NET cannot get from the
request stream. When a form is submitted, the textboxes and other
controls attach their name,value pairs onto the request stream, so that is
where .NET reads the 'new values' for all controls from. .NET knows this
data will be there, so it doesn't duplicate it into ViewState (especially
since ViewState would not have the correct value, it's not modified on the
client, it's static content itself, so it doesn't 'know' what the user has
just entered or modified on the screen)

Think of ViewState as having anything that isn't normally in a form POST
request, such as the text value of labels, e.g.

BTW, EnableViewState is true on controls by default, which is why you
don't have to include it as an attribute on a control. When you view a
control's properties in the Properties Window, only those that are
modified from their default values (they'd then turn bold in that window)
are added as attributes in the .aspx by VS.NET. You can of course
manually add them in as well...
 
H

Henri

Thanks for your answer Craig.

Anyway, this behavior doesn't suit my needs:
In my app, user can switch between different panels, so my TextBox can be
displayed, hidden, then displayed again, but it has to keep trace of what
user has entered in it.
But when the textbox is hidden, the input field is not rendered and only
ViewState would be helpful to maintain its value.
When you programmatically assign a property such as ForeColor, it's saved in
ViewState. It's too bad that this can't be done with the Text property.
That's why I'll have to do it by myself I think.

Henri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top