How to save data via postback?

I am working on an ASP.NET/C# site.

I am reading data from a database, storing it in a dictionary

Dictionary<string, decimal> Results 

and then linking it to an ASP.NET diagram

 PieChart.Series["Series"].Points.DataBind(Results, "Key", "Value", string.Empty); 

I want to change the point label when I click the button.

 protected void Button_Click(object sender, EventArgs e) { PieChart.Series["Series"].Points[0].Label = "abc" } 

But the problem is, when I click the button, a PostBack appears, and the data stored in the Dictionnary "Results" is lost, as well as the Chart.

Is there a way, rather than losing data, when a postback occurs without re-reading from the database?

Thanks for any help.

+7
source share
6 answers

Yes Use ViewState to save data between postbacks.

 public Dictionary<string, decimal> Results { get { return ViewState["Results"]; } set { ViewState["Results"] = value; } } 

Note: Check the Null value for viewstate, otherwise it will throw an exception or error.

+9
source

You can put data in a ViewState or Session to then pull it out "the other way."

+3
source

Some respondents suggested saving data to ViewState. Although this is configurable in ASP.NET, you need to make sure that you absolutely understand the consequences if you want to go this route, as this can severely damage performance. For this, I would recommend reading a TRULY understanding of ViewState .

Typically, storing datasets retrieved from a database in ViewState does degrade performance. Without knowing the details of your situation, I would jeopardize that you better just download the data from the database for each request. In essence, you have the opportunity to: a) serialize data and send data to the client (who can be on a slow connection) or b) get data from a database that is optimized for data extraction and smart caching.

+2
source

A better solution than using ViewState and transferring this data back and forth from the client could be to create a client identifier that each one goes back and forth and stores a cache of this data on the server side using this identifier, so you do not need to send this data from client to server every time, just the opposite.

This still sounds wrong to me, but it is a better solution than some other answers that are associated with a lot of overhead due to back and forth data.

In fact, since you are only changing the displayed information, and, from your question, I do not believe that you are really processing this data in any way, it seems to me that this really works for some kind of javascript along with your ASP.NET page. I have never done this, but some basic googling have displayed some articles about this.

+1
source

Instead of using ViewState, why don't you move your code to assign a value to the dictionary of the function, and then call this function from page_load with every postback. This way you will not lose data from your chart.

0
source

I often use asp: HiddenField to store small amounts of insecure data on the client side.

In the Page_Load () function, you can set the value of the hidden field, and then you can read it back in functions that are called later.

Do not use this for secure data, as the client user can use View Source to view the data on the page if they wish.

0
source

All Articles