Thursday, March 22, 2012

Maintaining value of variables between postback

Dear Sir,

I have created a Custom class to encapsulate an entity say City. I have another class called Cities which is a collection of Cities. In the class Cities, I basically have a HashTable. The key of the hashtable is the CityCode and the object is a City class.

I have methods to fetch sets of cities of different kinds from the database and can prepare the instances of Cities and within each each Cities, instances of City.

I now want to display the Cities in a DataGrid and allow the user to modify or delete or define new city. For this I wanted the class Cities to implement IList. But I have run into lots of trouble and will discuss this later. To bypass this, I have methods which can return array of city objects from Cities. This array I attach as the DataSource for the DataGrid. The display happens fine this first time. When I modify a City and the page does a postback, all the data in the variable storing my Cities is initialised. How can I overcome this problem? Do we have something like viewstate for the variables as well? I cannot use Static variables in this context for obvious reasons.

Thanks in advance.

Regards,

Do you have the DataGrid's ViewState turned on? That should persist the display.

You can always store your collection into Session state:

To store the collection:
Session["CitiesData"] = yourCitiesObject;

Later to retrieve the collection:
object sessionObject = this.Session["CitiesData"];
Cities yourCitiesObject = null;
if ( sessionObject != null )
yourCitiesObject = (Cities)sessionObject;

NC...


Dear Sir,

Thank you for the solution.

Please let me know how can we clear the data held in the Session object when the user moves out of the page.

Thanks in advance.

Regards,


Either Session.Remove("CitiesData") or Session["CitiesData"] = null should work.

NC...


How about using ViewState?

Public Property Cities()As Object Get Dim oAs Object =CType(ViewState("Cities"),Object)Return oEnd Get Set(ByVal ValueAs Object) ViewState("Cities") = ValueEnd Set End Property
This will be lost when the user closes the browser which is a requirement I'm assuming your also looking for?

From the sounds of his "Cities" structure, I would think that it would be a little much to place into ViewState.

NC...


Dear Sir,

Thank you for your responses.

I am aware that I can remove a Session variable using the sysntax as mentioned. The question is when I need to use the command. As far as I can think, the use can move out of the page by

1. pressing the BACK or PREVIOUS commands. (I can program these to point to release the Session variable)

2. providing another URL. (Presently, I am not concerned with this as user will have difficulty in getting back to the application and if he logs in once again the application can start with a fresh session)

3. selecting another menu option of the application. (Here is the major problem. All the menu button presses will have to be programmed to free the not required session variables)

I cannot think of any other way.

I am not considering the situation of the user closing the Browser because then the session gets cleared as it is (Am I right?).

Is there is tested and proved mechanism to handle this situation. I am grateful for the "ViewState" possibility. I will study and find out further. Our application is intended to be used by about 1000 dealers and about 100 back office staff of a Telecom Operator. SO, we are looking for the best solution that we can provide.

Thanks in advance.

Regards,


The Session automatically clears itself after a set time (Session.Timeout) with no activity, regardless of if the user navigates away, closes their browser, etc. The default is 20 minutes.

NC...

0 comments:

Post a Comment