473,320 Members | 1,952 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,320 software developers and data experts.

How to retrieve control's value from ViewState

I'm responding to a button click event on an asp.net web form. I then need
to retrieve the value from a TextBox control and I want to compare it
against the control's previous value to see if it has changed. How can I
retrieve a control's previous value from the ViewState? I know that I could
save the control's previous value in a session variable, reretrieve it's
value from the db, etc. But it would appear the control's previous value
would be in the ViewState since that is what ASP.NET uses to fire events
such as TextChanged.

Thanks!
Nov 19 '05 #1
2 3906
Are you asking "how do I pull from viewstate?" ViewState is a statebag
object, which has key/value pairs. To pull from viewstate, you can pull
directly from ViewState:

//direct pull
string value = (string) ViewState["keyName"];

The key here is a) pulling the correct value and b) pulling at the right
time (before it is reset). This is rather easy, as the order is (i.e., are
you asking "when can I pull from Viewstate?"):

Page.Init
Page.LoadViewState
Page.ProcessPostData
Page.Load
Change events for controls
Server side validation
Button.Click or Button.Command events
Page.PreRender
Page.SaveViewState <<< This is where it is too late to pull the old value
Page.Render

As long as you grab the old value before Page.SaveViewState, you are fine.

As a poor man's option, you can also save the value to ViewState as another
value:

ViewState["ControlCurrentValue"] = ControlName.Text;

You can then pull this and check. NOTE, however, that this is not wise for
all controls on a page, esp. a large page, as ViewState will get very large.
It is less of a problem in 2.0, but still a problem. This is necessary if you
are pulling the value at Render (see no reason to do this, of course).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"epigram" wrote:
I'm responding to a button click event on an asp.net web form. I then need
to retrieve the value from a TextBox control and I want to compare it
against the control's previous value to see if it has changed. How can I
retrieve a control's previous value from the ViewState? I know that I could
save the control's previous value in a session variable, reretrieve it's
value from the db, etc. But it would appear the control's previous value
would be in the ViewState since that is what ASP.NET uses to fire events
such as TextChanged.

Thanks!

Nov 19 '05 #2
I was asking the first question, although the answer to the second one is
key! Thanks. The thing I am trying to do is to get to the actual control
that is stored in the ViewState (or it's text value), not an object that I
placed into the ViewState statebag. I tried doing this, in my button click
event handler, with code such as:

TextBox tbxSaved = (TextBox)ViewState["myTextBoxId"];

where myTextBoxId is the value of the id attribute of the TextBox control on
the form. It comes back as <undefined value>. Any ideas what I am doing
wrong or if this is possible?

Thanks.
"Cowboy (Gregory A. Beamer) - MVP" <No************@comcast.netNoSpamM> wrote
in message news:24**********************************@microsof t.com...
Are you asking "how do I pull from viewstate?" ViewState is a statebag
object, which has key/value pairs. To pull from viewstate, you can pull
directly from ViewState:

//direct pull
string value = (string) ViewState["keyName"];

The key here is a) pulling the correct value and b) pulling at the right
time (before it is reset). This is rather easy, as the order is (i.e., are
you asking "when can I pull from Viewstate?"):

Page.Init
Page.LoadViewState
Page.ProcessPostData
Page.Load
Change events for controls
Server side validation
Button.Click or Button.Command events
Page.PreRender
Page.SaveViewState <<< This is where it is too late to pull the old value
Page.Render

As long as you grab the old value before Page.SaveViewState, you are fine.

As a poor man's option, you can also save the value to ViewState as
another
value:

ViewState["ControlCurrentValue"] = ControlName.Text;

You can then pull this and check. NOTE, however, that this is not wise for
all controls on a page, esp. a large page, as ViewState will get very
large.
It is less of a problem in 2.0, but still a problem. This is necessary if
you
are pulling the value at Render (see no reason to do this, of course).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"epigram" wrote:
I'm responding to a button click event on an asp.net web form. I then
need
to retrieve the value from a TextBox control and I want to compare it
against the control's previous value to see if it has changed. How can I
retrieve a control's previous value from the ViewState? I know that I
could
save the control's previous value in a session variable, reretrieve it's
value from the db, etc. But it would appear the control's previous value
would be in the ViewState since that is what ASP.NET uses to fire events
such as TextChanged.

Thanks!

Nov 19 '05 #3

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

Similar topics

1
by: nic | last post by:
Hi, I have an aspx page where I dynamically generate a number of input controls. Upon submitting the form I need to re- create the controls in order to retrieve their values. I understand this,...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
4
by: louise raisbeck | last post by:
Hi I know there are many posts on this but cant seem to find simple answer. I have lots of text boxes in a datalist edittemplate control. i dont want to do an update statement for all of them, as...
0
by: epigram | last post by:
I'm responding to a button click event on an asp.net web form. I then need to retrieve the value from a TextBox control and I want to compare it against the control's previous value to see if it...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
3
by: Jordan | last post by:
My ASP.NET 1.1. app dynamically loads a user control into a PlaceHolder control that exists in an aspx page. The user control is loaded during the Page_Load event of the aspx. The PlaceHolder's...
4
by: Mark Olbert | last post by:
This involves a family of related, databound ASPNET2 composite controls. I've managed to arrange things so that the composite controls restore themselves from ViewState on postback after they're...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
1
by: shapper | last post by:
Hello, I have a class where I created various controls. One of the controls have a property which is a generic list of WebControl. Then in web site page I have something like: Dim a As New...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.