每个页面请求,甚至是回发,我在Page_Init事件中填充变量,但在DropDownList SelectedIndexChanged事件中需要时(在同一请求中)它为空。
我的代码(用户控件)看起来像这样......
public partial class CourseInfoDetail : System.Web.UI.UserControl
{
protected List<CRS_vwCourse2> _offerings;
protected bool _test;
protected void Page_Init(object sender, EventArgs e)
{
_test = true;
using (CRSDataContext dc = new CRSDataContext(Config.ConnString))
{
List<CRS_vwCourse2> _offerings = (my query here).ToList();
//code removed here, but _offering is not used for anything other than displaying data here
我的事件处理程序代码很简单......
protected void ddlLocationId_SelectedIndexChanged(object sender, EventArgs e)
{
//_test is true here
//_offerings is null here
如果我将代码移动到Page_Load事件,它可以正常工作,_offerings在DropDown处理程序中具有正确的值,但我的其他代码不起作用,因为某些控件需要在Page_Init中重新填充,所以它们在回发时就在那里数据由ASP.NET填充。
请注意,我会在每次回发时填充List变量,因为我不想保留它。
答案 0 :(得分:2)
从
中删除List<CRS_vwCourse2>
List<CRS_vwCourse2> _offerings = (my query here).ToList();
您在_offerings
内声明了局部变量Page_Init
,而班级成员_offerings
仍然未分配。