2013-05-09 56 views
-2

在我的asp.net頁面中,我使用ajax,並且我想知道如何在進行ajax調用時保留變量數據。基本上在頁面加載時,我從數據庫下載大量信息並將其顯示在頁面上。然後在ajax調用期間,我想使用存儲在變量中的數據,但問題是,在ajax調用期間,數據消失了,我必須重新下載數據。如何在asp.net中的ajax調用期間保持數據?

有沒有辦法在ajax調用期間保持它?

+2

你能發佈您的代碼,並可能更闡述?我對這個有點困惑。 – tymeJV 2013-05-09 19:14:53

回答

1

在ASP.net你有很多方法可以做到這一點,Session["YourNameHere"]你有谷,直到使sesion被關閉或ViewState["YourNameHere"]你在頁面的值,直到它被關閉。

一些鏈接Using ViewStateUsing SesionState 我希望這可以幫到你。

編輯

使用視圖狀態。

  1. 首先把你的課程序列化。

    [Serializable] 
    public class Fruits 
    { 
        public string Apple { get; set; }} 
    
  2. 保存該值:

    Fruits Fruit = YourMethod(); //Yor method that retrive the data from DataBase* ViewState["Fruit"] = Fruit;

  3. 當你使用ViewState的:

    Fruits Fruit = ViewState["Fruit"]

+0

我想使用viewstates,但我不知道如何在其中保存複雜的對象。我在這裏做了另一個線程http://stackoverflow.com/questions/16470510/how-to-searalize-complex-objects-and-put-them-in-a-viewstate-in-asp-net。 – omega 2013-05-09 20:33:46

+0

1秒,我會更新答案。 – 2013-05-09 20:38:09

1
I want to use the data stored in the variable, but the problem is, during the ajax call, that data goes away, and I have to re download the data again. 

這不是問題,它是http和無狀態環境的本質。

根據檢索到的數據類型,有一些緩存值的機制: 應用程序狀態,會話,緩存,cookie,靜態字典。這實際上取決於數據的背景,使用場景,硬件等。

最好的方法是避免緩存對象,直到知道這是系統或此特定功能的瓶頸。

+0

真的,在任何ajax或回發請求中,您將失去會話及其所有數據,並將以新的數據開始? – omega 2013-05-09 19:19:59

+0

一個字:是的。這就是http這樣的無國籍環境的本質。 – 2013-05-10 03:33:11

相關問題