如何通过回发保留数据?
我正在开发 ASP.NET/C# 网站。
我正在从数据库中读取数据,将其保存在字典中
Dictionary<string, decimal> Results
,然后将其绑定到 ASP.NET 图表。
PieChart.Series["Series"].Points.DataBind(Results, "Key", "Value", string.Empty);
我想在单击按钮时更改点的标签。
protected void Button_Click(object sender, EventArgs e)
{
PieChart.Series["Series"].Points[0].Label = "abc"
}
但是,当我单击按钮时,会发生回发,并且保存在“结果”字典中的数据以及图表都会丢失。
有没有一种方法可以在回发时不丢失数据,而无需再次从数据库中读取数据?
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是 使用ViewState在回发之间保留数据。
注意:
检查 viewstate 的 Null 值,否则会抛出异常或错误
Yes Make use of ViewState to preserve data between postback.
Note:
Check for the Null value of viewstate otherwise it will throw an Exception or Error
一些响应者建议将数据存储在 ViewState 中。虽然这是 ASP.NET 中的自定义操作,但如果您想走这条路,您需要确保您完全理解其中的含义,因为它确实会损害性能。为此,我建议阅读真正理解ViewState< /a>.
通常,将从数据库检索的数据集存储在 ViewState 中确实会损害性能。在不了解您情况的详细信息的情况下,我会冒险猜测您最好在每次请求时从数据库加载数据。本质上,您可以选择 a) 序列化数据并将数据发送到客户端(连接速度可能较慢)或 b) 从数据库检索数据,数据库针对数据检索和智能缓存进行了优化。
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.
您可以将数据放入
ViewState
或Session
中,然后能够在“另一侧”将其拉出。You can put the data in
ViewState
orSession
to then be able to pull it out "on the other side".比使用 ViewState 并从客户端来回传递此数据更好的解决方案可能是创建一个客户端 id,每个客户端 id 都来回传递,并在服务器端保留此数据的缓存,并以此 id 为键。这样,您就不需要每次都将这些数据从客户端发送到服务器,只需相反。
这对我来说听起来仍然是错误的,但它比其他一些答案更好,因为数据来回会涉及大量开销。
事实上,由于您只是更改显示信息,并且从您的问题来看,我不相信您实际上正在以任何方式处理这些数据,在我看来,这确实是某种 javascript 与您的工作一起的工作。 ASP.NET 页面。我从来没有这样做过,但一些基本谷歌搜索确实出现了一些<有关以下内容的 href="http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx" rel="nofollow">文章 这。
A better solution than using the ViewState and passing this data back and forth from the client may be to create a client id that each is passed back and forth and keep a cache of this data on the server side, keyed by this id. That way you do not need to send this data from client to server each time, only the other way around.
This still sounds wrong to me, but it is a better solution than some of the other answers that would involve so much overhead due to the data back-and-forth.
In fact since you're only changing display information and, from your question, I don't believe you're actually processing this data in any way, it seems to me that this is really a job for some sort of javascript alongside of your ASP.NET page. I have never done this, but some basic googling did turn up some articles about this.
与其使用 ViewState,为什么不将用于为字典赋值的代码移至一个函数,然后在每次回发时从 page_load 调用该函数。这样您就不会丢失图表中的数据。
Instead of using ViewState, why dont you move your code for assigning value to the dictionary to a function and then call that function from page_load on every postback. This way you will not lose data from your chart.
我经常使用 asp:HiddenField 在客户端存储少量的非安全数据。
在 Page_Load() 函数中,您可以设置隐藏字段值,然后可以在稍后调用的函数中读回它。
不要将其用于安全数据,因为客户端用户可以根据需要执行“查看源”来查看页面中的数据。
I often use asp:HiddenField to store small amounts of non-secure data on the client side.
In the Page_Load() function you can set the hidden field value, and then you can read it back in functions that get called later.
Do not use this for secure data as the client user can do a View Source to see the data in the page if they wish.