[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Saving the state of User Control's variable(HashTable) on a web page

Posted on 2007-08-10
11
Medium Priority
?
865 Views
Last Modified: 2013-11-26
I'm using VS 2005, ASP.Net with C#.

I have a web user control on a web page and that user control has a HashTable. When the page postback the data filled HashTable is become null. What could be the solution? Where I have to keep the HashTable and how?
And there are lots of control that do post back on my page.
Thanks,
0
Comment
Question by:kosturdur
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 19670462
Make sure you are populating the data on each postback, but not setting any values of input fields.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1400 total points
ID: 19671020
You need to persist the Hashtable in something like a Session variable to save the value between post-backs.

Bob
0
 
LVL 18

Assisted Solution

by:DropZone
DropZone earned 600 total points
ID: 19671066
You can store the HashTable in the ViewState or the SessionState.

In your User Control, instead of defining a local member to store the HashTable, use the ViewState instead.  For example, if you have a property called "HashList", change its code to:

public string HashList
{
    get
    {
        HashTable HashList = (HashTable) this.ViewState["HashList"];
        return HashList;
    }
    set
    {
        this.ViewState["HashList"] = value;
    }
}
 
   -dZ.
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Author Comment

by:kosturdur
ID: 19671250
Hi strickdd,
The HashTable keeps the appearance properties of a grid that is on the page.Not same but like;

HashTable GridColumsOptions = new HashTable();
ColumnAppearanceeOptions columnOptions = new ColumnAppearanceOptions(visible,showfooter,sorting)
GridColumsOptions .Add(column.name,columnOptions)

I want to fill the HashTable from database at the first time only. But If I have to fill the HashTable with every postback so there is no  need a Hashtable as I could get the data from database on every postback.

Do you think so? Will be speed(perfonmance) decrease? Because the data on the grid is large? Is there another solution?
Thanks,
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19671396
Thanks DropZone, TheLearnedOne
In DropZone's example, as there are 10-15 events(controls) that can do postback on my page, do I have to set HashTable(put into theviewstate) on every postback?
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19671473
No, fill the Hashtable once, store it in a Session variable, and retrieve it in the Page_Load when IsPostBack = True.

Bob
0
 
LVL 18

Expert Comment

by:DropZone
ID: 19671934
In my code, you only need to set it once, when you databind.  It will be restored as long as the control exists in the page.  You can alternatively store it in the Session State, as TheLearnedOne said, and then it will be accessible to every other component for that session.

The difference is that the ViewState is the persistence object for the control itself, while the Session State is for the entire session.  If you use multiple controls in your page of the same UserControl, you will need to store all their HashTables individually in the Session State.  If you use the ViewState, solution each one will store its own.  Either way, it depends on what you want to do.

     -dZ.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19672197
Since the ViewState is UTF-16 XML text encoded as a Base-64 string, I would think about the size of the Hashtable, because it will degrade the page load performance pretty seriously for a large table.

Bob
0
 
LVL 18

Expert Comment

by:DropZone
ID: 19672252
I agree.  I had forgotten about this because I do not send the ViewState to the client, I persist it in the server.  But it is really not practical to submit a large serialized structure to the client using ViewState.

   -dZ.
0
 
LVL 5

Author Comment

by:kosturdur
ID: 19673906
The HashTable's data  isn't big and I have only one HashTable per page.  In the web site there are at least 5 (up to 20) pages all of them has large grid reports. I wanted to keep all grid's appearance data(~15 columns) in separate HashTables but now I'm thinking to get and set all the appearance data from database. By this way the updates(changes) are simultaneous and while getting this data I don't need Session variable and ViewState. Is this a better decision?
Thanks,
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19674325
Persisting in a database is a good solution, but not a perfect one, since there may be latency problems with accessing the database and reading/writing data.  Usage will tell you whether that is a good decision or not.

Bob
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question