[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Jay

1/4/2003 2:55:00 PM

I would like to be able to programmatically control some
aspects of ViewState. If I do a Response.redirect from
Page1 to Page2 I would like to be able to save the
ViewState in a Session variable or in SQL Server. Then
when I return to Page1 via a redirect from Page2 I would
like to reload the saved view state for Page1.

Manually mainting state of my controls on Page1 via
Session variables would be very difficult. For Check
boxes, radio buttons, etc. manually maintaining state
would be easy. However, I have a DataGrid with a template
column of check boxes. As long as I am doing PostBacks
the check boxes maintain their state (checked or
unchecked). Curiously, the check boxes maintain their
state even when the EnableViewState for the DataGrid is
false which is exactly what I want.

Right now I have included in one page 3 DataGrids and many
controls so that I can use ViewState. In effect, to the
user I have several different pages which I implement by
showing and hiding. That one page has become unwieldy and
the performance is suffering. I would like to break it
into 3 different pages, but it would be extremely useful
to be able to save and restore ViewState programmatically
and not have to setup my own system for maintaing state.

Thanks for any help.

Jay
1 Answer

Jesse Ezell

1/4/2003 9:27:00 PM

0

This is not what ViewState is intended to do. You can
transfer a lot of the current requests information to
another page with Server.Transfer. Heavy use of ViewState
is not recommended either. It is mainly for storing minor
bits of information, so with your datagrids all using
ViewState, I can see how it might be a problem with 3
datagrids.

What you might want to do is store the data that is being
binded in session, along with some other info that you
need such as current page, selected index, etc. (this is
called a 'Memento' pattern...you can read about it "Gang
of Four" [Design Patterns, Eric Gamma]...the best books
ever written on software patterns, a highly recommended
read). Then, you don't have to run the query over an over
again.

A class like this would do the trick:

public class DataGridState
{
public string ID;
public int CurrentPage;
public int SelectedIndex;
public object DataSource;
public string CurrentSortExpression;
}

Of course, only include the bare minimum that you need to
restore the state.

Now, if you want to automate the process, or make it so
that it works across many types of objects without much
additional code, you could create a custom viewstate to
memento transfer class that uses reflection and
attributes to handle syncronization of your mememento
object with the datagrid and the session.

--Jesse