Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Saving list<> across postbacks

 
Thread Tools Search this Thread
Old 11-06-2009, 04:46 PM   #1
Default Saving list<> across postbacks


I have a page that is using a List collection (list<>) and I want to save it
across postbacks.

I tried adding it to my ViewState but get an error:

Type 'AutoUPS.ClientList' in Assembly 'AutoUPS, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' is not marked as serializable.

How do I set this up so I can save it?

In my web page I am doing:

public partial class AutoUPS : System.Web.UI.Page
{
private int exceptionDrop;
public static ClientList clientList = new ClientList();

...

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else
{
clientList = (ClientList)ViewState["ClientList"];
}
}

protected override void OnPreRender(EventArgs e)
{

base.OnPreRender(e);
ViewState["ClientList"] = clientList;
}

I am calling this from another page so I have my ClientList set as a static
and then want to save it so I don't have to call the database all the time.

Not sure if this is the best way but it seemed so.

Thanks,

Tom




tshad
  Reply With Quote
Old 11-06-2009, 05:21 PM   #2
Göran Andersson
 
Posts: n/a
Default Re: Saving list<> across postbacks
tshad wrote:
> I have a page that is using a List collection (list<>) and I want to save it
> across postbacks.
>
> I tried adding it to my ViewState but get an error:
>
> Type 'AutoUPS.ClientList' in Assembly 'AutoUPS, Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null' is not marked as serializable.
>
> How do I set this up so I can save it?
>
> In my web page I am doing:
>
> public partial class AutoUPS : System.Web.UI.Page
> {
> private int exceptionDrop;
> public static ClientList clientList = new ClientList();
>
> ...
>
> protected void Page_Load(object sender, EventArgs e)
> {
> if (!Page.IsPostBack)
> {
> }
> else
> {
> clientList = (ClientList)ViewState["ClientList"];
> }
> }
>
> protected override void OnPreRender(EventArgs e)
> {
>
> base.OnPreRender(e);
> ViewState["ClientList"] = clientList;
> }
>
> I am calling this from another page so I have my ClientList set as a static
> and then want to save it so I don't have to call the database all the time.
>
> Not sure if this is the best way but it seemed so.
>
> Thanks,
>
> Tom
>
>


To make the class serializable, you add the [Serializable()] attribute
to it. It also requires the class to have properties that are both
readable and writable, and the class needs a parameterless constructor
so that the deserializing can recreate an instance.

However, putting the list in ViewState means that you are sending the
serialized data to the browser and back, which makes it slower to load
and postback.

You could save the list in a Session variable, then it would not be sent
to the client. Consider the memory usage though, if you use too much
memory for each session that limits the number of session that the site
can handle. You may want the limitation to be how many requests the
server can handle per second rather than an absolute limit on the number
of visitors.

--
Göran Andersson
_____
http://www.guffa.com


Göran Andersson
  Reply With Quote
Old 11-06-2009, 06:22 PM   #3
tshad
 
Posts: n/a
Default Re: Saving list<> across postbacks

"Göran Andersson" <> wrote in message
news:...
> tshad wrote:
>> I have a page that is using a List collection (list<>) and I want to save
>> it across postbacks.
>>
>> I tried adding it to my ViewState but get an error:
>>
>> Type 'AutoUPS.ClientList' in Assembly 'AutoUPS, Version=1.0.0.0,
>> Culture=neutral, PublicKeyToken=null' is not marked as serializable.
>>
>> How do I set this up so I can save it?
>>
>> In my web page I am doing:
>>
>> public partial class AutoUPS : System.Web.UI.Page
>> {
>> private int exceptionDrop;
>> public static ClientList clientList = new ClientList();
>>
>> ...
>>
>> protected void Page_Load(object sender, EventArgs e)
>> {
>> if (!Page.IsPostBack)
>> {
>> }
>> else
>> {
>> clientList = (ClientList)ViewState["ClientList"];
>> }
>> }
>>
>> protected override void OnPreRender(EventArgs e)
>> {
>>
>> base.OnPreRender(e);
>> ViewState["ClientList"] = clientList;
>> }
>>
>> I am calling this from another page so I have my ClientList set as a
>> static and then want to save it so I don't have to call the database all
>> the time.
>>
>> Not sure if this is the best way but it seemed so.
>>
>> Thanks,
>>
>> Tom

>
> To make the class serializable, you add the [Serializable()] attribute to
> it. It also requires the class to have properties that are both readable
> and writable, and the class needs a parameterless constructor so that the
> deserializing can recreate an instance.
>


All the fields (3) are int or string.

> However, putting the list in ViewState means that you are sending the
> serialized data to the browser and back, which makes it slower to load and
> postback.


I realize that, but I have a dropdown that has client names and clientIDs.
The problem is that I also need the account number to find a selection in
the dropdown (but only 2 values can be kept in a dropdown unless you
concatenate them but then you have another issue to deal with.

I just build a list<> with both the ID and Account# and if they put an
Account# (which is what they know) and then get the ClientID from the List<>
based on this Account# - I can then select the client in the dropdown. But
to do that I would need to keep the List<> around somewhere. It wouldn't be
very large.
>
> You could save the list in a Session variable, then it would not be sent
> to the client. Consider the memory usage though, if you use too much
> memory for each session that limits the number of session that the site
> can handle. You may want the limitation to be how many requests the server
> can handle per second rather than an absolute limit on the number of
> visitors.


Also, you have a time problem if they keep the page open (this is an office
app) but don't do anything. You would lose the sesssion info.

Also, whether you keep the session on the page or in sessions, you still
need to pass the data around, right?

Thanks,

Tom
>
> --
> Göran Andersson
> _____
> http://www.guffa.com





tshad
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't disable power saving mode donster Computer Support 0 03-24-2007 01:40 PM
re: browsers slow in saving paco@mexico.born Computer Support 0 01-07-2007 03:00 AM
Saving web page to disc m_novox@yahoo.com Computer Information 2 07-31-2006 10:50 PM
Saving webpages to disk, Mozilla Firefox/Win98SE, taking long time? Rolf Computer Support 2 06-10-2006 05:41 PM
Saving newsgroup binaries in OE problem berksposter Computer Support 13 09-27-2003 12:21 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46