变量填充在 Page_Init 中,但在 DropDownList SelectedIndexChanged 事件处理程序中为空
Variable populated in Page_Init but null in DropDownList SelectedIndexChanged event handler
每个页面请求,甚至回发,我都会在 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 变量,因为我不想保留它。
从
中删除List<CRS_vwCourse2>
List<CRS_vwCourse2> _offerings = (my query here).ToList();
您正在 Page_Init
内声明局部变量 _offerings
,并且 class 成员 _offerings
保持未分配状态。
每个页面请求,甚至回发,我都会在 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 变量,因为我不想保留它。
从
中删除List<CRS_vwCourse2>
List<CRS_vwCourse2> _offerings = (my query here).ToList();
您正在 Page_Init
内声明局部变量 _offerings
,并且 class 成员 _offerings
保持未分配状态。