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

ViewState / Own Object

Rob
Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob

Feb 12 '07 #1
6 1166
myObject = (MyObject) ViewState["myObject"];
myObject.Property = newValue;
ViewState["myObject"] = myObject;

What's wrong with that?

"Rob" <ba*********@googlemail.comwrote in message
news:11*********************@q2g2000cwa.googlegrou ps.com...
Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob

Feb 12 '07 #2
Would not it be more logical to keep it in a session variable?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Rob" <ba*********@googlemail.comwrote in message
news:11*********************@q2g2000cwa.googlegrou ps.com...
Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob

Feb 12 '07 #3

THings that go into the ViewState (or Session or Application) are objects.

as in

dim o as Object
object o = null;

When you pull them out, you check that the key is not null/nothing , then
you have to CAST it as the correct object.

...

string empKey = "EmployeeKey";

Employee emp = new Employee();
emp.Name = "John Smith";

ViewState[empKey ] = emp;

then later:
string empKey = "EmployeeKey";
Employee foundEmployee = null;
if(null!=ViewState[empKey ])

foundEmployee = (Employee) ViewState[empKey ];

}

if(null!=foundEmployee)
{
foundEmployee.Name = "Mary Jones";
}
--------VB>NET
Dim empKey As String = "EmployeeKey"

Dim emp As New Employee()
emp.Name = "John Smith"

ViewState(empKey) = emp

Dim empKey As String = "EmployeeKey"
Dim foundEmployee As Employee = Nothing
If not (ViewState(empKey) is nothing) Then

foundEmployee = CType(ViewState(empKey), Employee)
End If
If not (foundEmployee is nothing ) Then
foundEmployee.Name = "Mary Jones"
End If


"Rob" <ba*********@googlemail.comwrote in message
news:11*********************@q2g2000cwa.googlegrou ps.com...
Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob

Feb 12 '07 #4
Rob
Thanks all....

I had got a bit confused with the serialization and wondered if I
would need to de-serialize before I could update the object and then
reserialize - a colleague put me straight! :o)

Regarding the session - nope - as that would time out, using the
viewstate as long as they dont close the browser all data entered thus
far will remain until they complete the operation.

Regards

Rob

Feb 12 '07 #5
Unless your "object" is very small, you will probably be better off using
Cache with a named unuque key (Such as Session.SessionId.ToString() +
"username" ) You can set the Cache priority to never remove, and so you won't
have the problem of session expiration.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rob" wrote:
Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob

Feb 12 '07 #6
"Peter Bromberg [C# MVP]" wrote ...
Unless your "object" is very small, you will probably be better off using
Cache with a named unuque key (Such as Session.SessionId.ToString() +
"username" ) You can set the Cache priority to never remove, and so you
won't
have the problem of session expiration.
Hi Peter,

Thanks for the reply and info - I'm always open to new ideas, dont always
understand them at first though! :o)

The object is a UserRegistration object, is consists of about 30 variables
that hold strings, integers and a guid, also a couple of collection objects
which in turn hold strings/integers...

The object doesn't "feel" too big for the viewstate, and when I look at the
source, the text it generates is not particularly massive...

Any thoughts?

Rob
Feb 13 '07 #7

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

Similar topics

2
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
1
by: Ralph Soons | last post by:
Hi all, I am trying to save the viewstate in a session instead of storing it in a hidden of the webpage which is default. This because of performance reasons. When I use line 2 in combination...
6
by: clsmith66 | last post by:
Is it possible to store the same information about a control that would be saved in the ViewState in a Session state? I have a page with three treeview controls and if I enable the view state for...
9
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
2
by: Ryan Ternier | last post by:
I'm playing around with Cache and Viewstate. I've made a small little example that reminds me of grade 12: if(!IsPostBack) { ArrayList alTest = new ArrayList(); alTest.Add("This is from...
6
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
6
by: paul.hester | last post by:
Hi all, Does anyone know why the ViewState would be empty? When I'm receiving a postback, I can access a posted value using controlName.Value but not ViewState. I have EnableViewState set...
2
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.