Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep data through postbacks?

I am working on a ASP.NET/C# Website.

I am reading data from a database, saving it in a Dictionary

Dictionary<string, decimal> Results

and then binding it to a ASP.NET chart

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

I want to change the Label of a Point when I click a button.

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

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

Is there a way to , not lose the data when a postback happens without having to read from the database all over again?

Thank you for any help.

like image 759
Y2theZ Avatar asked Sep 12 '11 13:09

Y2theZ


1 Answers

Some responders have suggested storing the data in ViewState. While this is custom in ASP.NET you need to make sure that you absolutely understand the implications if you want to go down that route, as it can really hurt performance. To this end I would recommend reading TRULY understanding ViewState.

Usually, storing datasets retrieved from the database in ViewState really hurts performance. Without knowing the details of your situation I would hazard a guess that you are better off just loading the data from the database on every request. Essentially, you have the option of a) serializing the data and sending the data to the client (who could be on a slow connection) or b) retrieving the data from the database, which is optimized for data retrieval and clever caching.

like image 101
Rune Avatar answered Sep 30 '22 21:09

Rune