473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple ViewState question

Nad
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?
Nov 19 '05 #1
9 1270
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 4 Jul 2005 16:00:02 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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?


Nov 19 '05 #2
Nad
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)
"Scott Allen" wrote:
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 4 Jul 2005 16:00:02 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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?


Nov 19 '05 #3
On Tue, 5 Jul 2005 07:12:06 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #4
Nad
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?

"Scott Allen" wrote:
On Tue, 5 Jul 2005 07:12:06 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #5
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>
On Tue, 5 Jul 2005 11:49:01 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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?


--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #6
Nad
Got it. Thanks a lot for your time.

"Scott Allen" wrote:
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>
On Tue, 5 Jul 2005 11:49:01 -0700, Nad <Na*@discussions.microsoft.com>
wrote:
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?


--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #7
Hi,

I have also blogged some time ago about the thing Scott just explained. For
the reference, here it is:

ASP.NET: TextBox and EnableViewState = "False"
http://blogs.aspadvice.com/joteke/ar...03/15/767.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #8
Nad
Thank you. I learned.

"Teemu Keiski" wrote:
Hi,

I have also blogged some time ago about the thing Scott just explained. For
the reference, here it is:

ASP.NET: TextBox and EnableViewState = "False"
http://blogs.aspadvice.com/joteke/ar...03/15/767.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #9
Nad
Thank you, I learned.

"Teemu Keiski" wrote:
Hi,

I have also blogged some time ago about the thing Scott just explained. For
the reference, here it is:

ASP.NET: TextBox and EnableViewState = "False"
http://blogs.aspadvice.com/joteke/ar...03/15/767.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
2
by: Danny | last post by:
Hi, I have a very simple asp.net application with a webform and an integer data member. I've placed a button on this form which increments the integer data member by 1 each time it is clicked...
3
by: Kevin Gorski | last post by:
This question has been asked before, but there have been no definitive answers that I was able to find. Can the viewstate hidden input field be moved to a less prominent page location? On an...
1
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
3
by: Sahil Malik | last post by:
In VS2003 - add a page. Add a button Add a textbox. Set EnableViewState = False for all 3 above. Put some code behind on Button (anything). Run the sample - type something in the textbox -...
7
by: Tom wilson | last post by:
I have created a very simple example that doesn't work. Form1 contains a textbox and a button: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
1
by: Mark Olbert | last post by:
I have a "master" composite control which, in turn, holds an instance of a "detail" composite control (the "master" control will ultimately contain multiple instances of the "detail" control, but...
3
by: Justin | last post by:
I have aspx pages with 10 checkboxlist controls. total individual checkbox (the sum of individual checbox in those 10 checkboxlists) on the page is about 1710. You can imagine how large the...
9
by: =?Utf-8?B?TUNN?= | last post by:
I'm sure the answer to my question varies depending on the situation, but I am looking for a general "best practice". If I have an asp.net application and I load certain data from a database,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.