如何通过回发保留数据? [英] How to keep data through postbacks?

查看:63
本文介绍了如何通过回发保留数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用ASP.NET/C#网站.

我正在从数据库中读取数据,并将其保存在字典中

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

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

Dictionary<string,> Results



然后将其绑定到ASP.NET图表



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.

推荐答案

使用Session变量. Google是您的朋友.
Use Session variables. Google is your friend.


您可以在第一次从数据库中读取数据时对数据进行缓存.只需检查缓存对象是否仍然存在,以及是否将缓存对象在每个帖子上都投射回Dictionary.
看到这里:
http://asp.net-tutorials.com/caching/object-caching/ [ ^ ]

另一个新的智能方法是使用ajax代替回发,只是为了更改标签名称/值.
对于ajax,请参见此处:
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first [ ^ ]

这是许多人使用的通用ajax,但是根据您的经验,您希望深入了解Script Services,您可以在这里找到:
http://msdn.microsoft.com/en-us/magazine/cc163499.aspx [ ^ ]
You can cache your data the first time you read from the database. Just check if the cache object still exist and if it does you just cast the cache object back to the Dictionary on every post.
See here:
http://asp.net-tutorials.com/caching/object-caching/[^]

Another new smart approach is to use ajax instead posting back just to change a label name/value.
For ajax see here:
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first[^]

That''s the common ajax that many are using but depending on your experience you would rather want to dive into Script Services which you can find here:
http://msdn.microsoft.com/en-us/magazine/cc163499.aspx[^]


您可以在会话中存储结果.但是,Dictionary不能序列化,因此,如果您的会话存储到SQL Server(通常存储在内存中),则可能最终不得不使用
You could store Results in Session. Though, Dictionary isn''t serializable, so if your session is stored to SQL Server (usually it''s stored to memory), you may end up having to use something like a proxy dictionary.


这篇关于如何通过回发保留数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆