|
每人能回答吗?我想把代码贴上来的,可是东西不少。我决定只粘贴页面和和web control的page_load和page_init代码好了。问题可能出现在这两个地方。 前台的页面关系应该不大,所以就不贴代码了。 aspx的 c# code: public partial class ConfigCallType : System.Web.UI.Page { public Session session; protected void Page_Load(object sender, EventArgs e) { if (this.Page == null || this.Com == null) return; Object layer = Application["XpoLayer"]; session = new DevExpress.Xpo.Session(layer as IDataLayer); if (Page.IsPostBack == false ) { //for the first time AddObjectPopup.Windows.Add(new PopupWindow("loading", "Company")); AddObjectPopup.Windows.Add(new PopupWindow("loading", "SubCompany")); AddObjectPopup.Windows.Add(new PopupWindow("loading", "CallType")); } } protected void Page_Init(object sender, EventArgs e) { if (this.Page == null || Com == null) { if (this.Page != null && Page.IsPostBack == true) { this.Response.Clear(); this.Response.End(); }//for the bug that I can't solve if (this.Page != null && Page.IsPostBack == false) { Response.Redirect(Request.Url.ToString()); } //for the bug of logging out } Object layer = Application["XpoLayer"]; session = new DevExpress.Xpo.Session(layer as IDataLayer); //set up relation SubCom.ParentObject = Com; ct.ParentObject = SubCom; Com.PageUser = User.Identity.Name; SubCom.PageUser = User.Identity.Name; Com.LimitedByUser = true; SubCom.LimitedByUser = true; // set up event Com.BubbleRaise += new EventHandler(Com_BubbleRaise); SubCom.BubbleRaise += new EventHandler(SubCom_BubbleRaise); ct.BubbleRaise += new EventHandler(ct_BubbleRaise); } WebControl的代码: SimpleObject.ascx.cs ,上述页面中定义的Com, SubCom和ct都是该控件的对象。 protected void Page_Load(object sender, EventArgs e) { if (session == null) { System.Type type = this.Page.GetType(); FieldInfo pi = type.GetField("session", BindingFlags.Public | BindingFlags.Instance); if (pi != null) { session = (Session)pi.GetValue(this.Page); } } if (this.Page.IsPostBack == false) { BindObject(); } } 还有一部分web control的自定义事件的代码也一起贴上来了: protected void RefreshBtn_Click(object sender, EventArgs e) { BindObject(); BubbleUp(sender, e); } private void BubbleUp(object sender, EventArgs e) { EventHandler BubbleRaised = (EventHandler)Events[EventBubbleRaised]; if (BubbleRaised != null) BubbleRaised(this, e); } private static readonly object EventBubbleRaised = new object(); #region Event Handler public event EventHandler BubbleRaise { add { Events.AddHandler(EventBubbleRaised, value); } remove { Events.RemoveHandler(EventBubbleRaised, value); } } #endregion 其它的BindObject方法之类的代码感觉和问题关系不大就不贴了。代码里使用了devExpress,但是用的是XPO这个ORM,感觉和这种页面生命周期的问题应该没什么大关系。 问题再说一次,就是this.Page以及com,subCom等对象为null而报错。在page_init里出错的,一开始我是把page_init里的代码都放到page_load里的,但是也是同样地出错的。我就想不通了,凭什么AJAX updatepanel postback回来的时候this.Page都为null值?(在debug的watch里看到的)。 而且吧它也不是每次都为null,感觉如果每次点击前等一小会就没问题,像是等页面在上一次AJAX的东西完全处理完的感觉。 其中,下面的代码就是用来避免页面上出错的。用了Response.End(),就可以不报错,但是对事件的AJAX响应就没有了,客户端没有更新。所以只能是一个临时的workaround. if (this.Page == null || Com == null) { if (this.Page != null && Page.IsPostBack == true) { this.Response.Clear(); this.Response.End(); }//for the bug that I can't solve if (this.Page != null && Page.IsPostBack == false) { Response.Redirect(Request.Url.ToString()); } //for the bug of logging out }
|