Simple ViewState question

G

Guest

Hello,

I have a textbox control with EnableViewState=false
I also have a datalist within a Panel in the same form with
EnableViewState=false
The datalist is filled using an SqlDataAdaptor.

Why is it that the textbox retains its text value but the datalist doesn't
retain its items in roundtrips?
 
S

Scott Allen

The TextBox control is restoring it's Text value from the Request.Form
collection - the browser will always include form field values in a
postback.

The DataList object's Items collection doesn't have a form field to
live in - it needs ViewState to persist across postbacks.

Make sense?
 
G

Guest

Thank you Scott,

Then what is the purpose of EnableViewState in say TextBox control? (Well at
least it should be set to false by default)

And which objects/controls do not have a form field? (I would guess those
which cannot be contained in just one field)
 
S

Scott Allen

Thank you Scott,

Then what is the purpose of EnableViewState in say TextBox control? (Well at
least it should be set to false by default)

And which objects/controls do not have a form field? (I would guess those
which cannot be contained in just one field)

Hi nad:

One scenario where ViewState is useful to a TextBox is when using the
TextChanged event - because the TextBox object will keep the Text
value in ViewState so it can compare the previous value it's seen
against the new incoming value in the form input, and raise an event
if the two are not equal.
 
G

Guest

Scott,

I put a breakpoint inside TextChanged event of a TextBox control and
regardless of EnableViewState's value the debugger stops there when I change
the text.

If I didn't misunderstand you, wouldn't this invalidate what you said?
 
S

Scott Allen

Hi nad:

Very good investigative work :)

Let me clarify:

With EnableViewState=false the TextBox control can't remember the
value it saw on the previous postback, however the TextBox control
will know what it's initial value was (either an empty string or the
string in the Text attribute of <asp:TextBox>), and what the incoming
form value is.

Without ViewState, the control will raise the TextChanged on every
postback if the incoming text value is different than the initial
value (the initial value being the Text property set in <asp:TextBox>,
or an empty string if the Text attribute doesn't exist).

With ViewState on, the control will only raise TextChanged if the text
has changed since the previous postback.

Does that make more sense? I know you have a form to experiment with,
but here is another simple example you can use to see the difference.

<%@ Page language="c#" %>

<script runat="server">

private void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button1_Click fired<br>");
}

private void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("TextChanged fired<br>");
}
</script>

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox Text="foo"
id="TextBox1"
runat="server"
EnableViewState="false"
OnTextChanged="TextBox1_TextChanged"
/>
<asp:Button id="Button1"
runat="server"
Text="Button"
OnClick="Button1_Click"
/>
</form>
</body>
</HTML>
 
G

Guest

Got it. Thanks a lot for your time.

Scott Allen said:
Hi nad:

Very good investigative work :)

Let me clarify:

With EnableViewState=false the TextBox control can't remember the
value it saw on the previous postback, however the TextBox control
will know what it's initial value was (either an empty string or the
string in the Text attribute of <asp:TextBox>), and what the incoming
form value is.

Without ViewState, the control will raise the TextChanged on every
postback if the incoming text value is different than the initial
value (the initial value being the Text property set in <asp:TextBox>,
or an empty string if the Text attribute doesn't exist).

With ViewState on, the control will only raise TextChanged if the text
has changed since the previous postback.

Does that make more sense? I know you have a form to experiment with,
but here is another simple example you can use to see the difference.

<%@ Page language="c#" %>

<script runat="server">

private void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button1_Click fired<br>");
}

private void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("TextChanged fired<br>");
}
</script>

<HTML>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox Text="foo"
id="TextBox1"
runat="server"
EnableViewState="false"
OnTextChanged="TextBox1_TextChanged"
/>
<asp:Button id="Button1"
runat="server"
Text="Button"
OnClick="Button1_Click"
/>
</form>
</body>
</HTML>
 

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

Top