2012-07-16 38 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