2012-07-16 35 views
1

我有一個全局變量,我在ASP.NET網頁更新面板中引用。這裏是在更新面板中發生的事情:更新部分發生後,列表被刪除

accidentListType.Add(Convert.ToString(AccidentDescription.SelectedItem)); 
     if (Atfault.Checked) 
     { 
      accidentAtFault.Add("True"); 
     } 
     else accidentAtFault.Add("False"); 
     DateTime newDate = new DateTime(Convert.ToInt32(accidentyear.Text), Convert.ToInt32(accidentmonth.Text), 1); 
     accidentListDate.Add(newDate); 
     TestAccidentLabel.Text = "Success! " + newDate.ToString(); 

基本上會發生什麼是一個列表獲取另一個添加成員每次單擊按鈕時。但是,每次運行代碼時,新索引都會被神祕地刪除,所以當我將所有事故添加到數據庫時,都不會添加任何事故。而且我不能動態添加它們,因爲事故數據庫的其中一個輸入是來自另一個表的標識,當表創建時我將其拉出,所以我必須在所有表後添加它們。

任何人都可以幫忙嗎? P.S.這是我第一次發帖,所以很抱歉,如果它是混亂或任何東西。

+0

是否在每個會話之間刪除條目?如果是這樣,您的代碼將數據移入數據庫的位置在哪裏? – Botonomous 2012-07-16 16:37:07

+0

我甚至沒有試圖將條目移動到數據庫。我只是試圖填充最終將進入數據庫的列表。但每次我嘗試添加一個新條目時,它都會神祕地消失。在調試模式下,我使用立即窗口查看添加後Count變量遞增,但當退出處理程序代碼時,再次按下按鈕,我調用Count函數以查看Count爲零。 – 2012-07-16 16:41:36

+0

您是否在視圖狀態/會話/任何位置存儲accidentListType以在回發之間保留它? – 2012-07-16 16:44:28

回答

1

有兩件事你是失蹤。雖然你的列表是全局的,因爲在每次請求頁面對象被銷燬之前,除非你在Session中保留列表,否則值將不會持續。此外,您必須重新使用您在會話中保存的列表來添加任何新值。做如下。

//at the start of block 
//check if there is anything in Session 
//if not, create a new list 
if(Session["accidentListType"] == null) 
    accidentListType = new List<string>(); 
else //else a list already exists in session , use this list and add object to this list 
    accidentListType = Session["accidentListType"] as List<string>; 

//do your processing, as you are doing 

//and at the end of bloack store the object to the session 
Session["accidentListType"] = accidentListType 
+0

沒有這樣的運氣。我嘗試過,但即使Session值也一直保持清除狀態。 – 2012-07-16 17:21:47

+0

你可以在這裏發佈你的整個代碼... – Anand 2012-07-16 17:26:59

+1

沒關係,修復它。那是對的。它只是不斷重新初始化爲空。非常感謝! – 2012-07-16 18:09:31