如何在用户控件属性中使用ViewState?
想一次Load后,一直能够使用的全局变量,在回传的时候能够不消失。
结果每次都会重置,查找资料发现需要重写ViewState的一些操作。
//Persist Viewstate protected override void LoadViewState(object savedState) { object[] viewState = (object[])savedState; ArrayList al = viewState[0] as ArrayList; if (al != null) { _formID = (int)al[0]; _configID = (int)al[1]; _formPeriodNo = (int)al[2]; _planPeriodNo = (int)al[3]; _formMonthStr = (string)al[4]; _planMonthStr = (string)al[5]; _webUrl = (string)al[6]; _isInPeriod = (bool)al[7]; } base.LoadViewState(viewState[1]); } protected override object SaveViewState() { if (System.Web.HttpContext.Current == null) return null; ArrayList al = new ArrayList(); al.Add(FormID); al.Add(ConfigID); al.Add(FormPeriodNo); al.Add(PlanPeriodNo); al.Add(FormMonthStr); al.Add(PlanMonthStr); al.Add(WebUrl); al.Add(IsInPeriod); object[] viewState = new object[2]; viewState[0] = al; viewState[1] = base.SaveViewState(); return viewState; }
神在人间,自由自在!