2011-03-24 104 views
0

我需要一种以持久方式(Session)保存和加载页面状态的方法。我需要这个项目是一个Intranet Web应用程序,它有几个配置页面,如果他们即将被保存,其中一些需要确认。确认页面必须是单独的页面。由于我受到限制,JavaScript的使用是不可能的。这是我能想出迄今:ASP.NET:如何坚持页面状态跨页面?

ConfirmationRequest:

[Serializable] 
public class ConfirmationRequest 
{ 
    private Uri _url; 
    public Uri Url 
    { get { return _url; } } 

    private byte[] _data; 
    public byte[] Data 
    { get { return _data; } } 

    public ConfirmationRequest(Uri url, byte[] data) 
    { 
     _url = url; 
     _data = data; 
    } 
} 

ConfirmationResponse:

[Serializable] 
public class ConfirmationResponse 
{ 
    private ConfirmationRequest _request; 
    public ConfirmationRequest Request 
    { get { return _request; } } 

    private ConfirmationResult _result = ConfirmationResult.None; 
    public ConfirmationResult Result 
    { get { return _result; } } 

    public ConfirmationResponse(ConfirmationRequest request, ConfirmationResult result) 
    { 
     _request = request; 
     _result = result; 
    } 
} 

public enum ConfirmationResult { Denied = -1, None = 0, Granted = 1 } 

Confirmation.aspx:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Request.UrlReferrer != null) 
     { 
      string key = "Confirmation:" + Request.UrlReferrer.PathAndQuery; 
      if (Session[key] != null) 
      { 
       ConfirmationRequest confirmationRequest = Session[key] as ConfirmationRequest; 
       if (confirmationRequest != null) 
       { 
        Session[key] = new ConfirmationResponse(confirmationRequest, ConfirmationResult.Granted); 
        Response.Redirect(confirmationRequest.Url.PathAndQuery, false); 
       } 
      } 
     } 
    } 

PageToConfirm.aspx:

private bool _confirmationRequired = false; 

    protected void btnSave_Click(object sender, EventArgs e) 
    { 
     _confirmationRequired = true; 
     Response.Redirect("Confirmation.aspx", false); 
    } 

    protected override void SavePageStateToPersistenceMedium(object state) 
    { 
     if (_confirmationRequired) 
     { 
      using (MemoryStream stream = new MemoryStream()) 
      { 
       LosFormatter formatter = new LosFormatter(); 
       formatter.Serialize(stream, state); 
       stream.Flush(); 

       Session["Confirmation:" + Request.UrlReferrer.PathAndQuery] = new ConfirmationRequest(Request.UrlReferrer, stream.ToArray()); 
      } 
     } 
     base.SavePageStateToPersistenceMedium(state); 
    } 

我似乎无法找到一种方法,从Confirmation.aspx到PageToConfirm.aspx被重定向,任何人都可以帮助我在这一个后加载页面国家?

+0

我已经解决了这个问题,如果有人需要解释只是在这里评论 – haze4real 2013-01-23 15:25:31

回答

1

如果你的意思是视图状态,请尝试使用Server.Transfer代替Response.Redirect

如果设置preserveForm参数 为true,目标页面将能够 使用 PreviousPage属性来访问 前一页的视图状态。

+0

我不需要在Confirmation.aspx我需要的视图状态视图状态如果我返回到PageToConfirm.aspx。 – haze4real 2011-03-24 10:03:38

+0

你可以做一个示例如何使用Server.Transfer持久页面状态?流程应该是这样的:Page1.aspx - > Page2.aspx - > Page1.aspx。 Page1.aspx的状态应与转换到Page2.aspx之前相同,例如如果您已将某些内容放入Page1.aspx上的文本框中,那么您在浏览Page2.aspx后应将其放在那里 – haze4real 2011-03-24 10:16:26

0

使用此代码能正常工作形成了我

public class BasePage 
{ 

protected override PageStatePersister PageStatePersister 
    { 
     get 
     { 
      return new SessionPageStatePersister(this); 
     } 
    } 
protected void Page_PreRender(object sender, EventArgs e) 
    { 
     //Save the last search and if there is no new search parameter 
     //Load the old viewstate 

     try 
     { //Define name of the pages for u wanted to maintain page state. 
      List<string> pageList = new List<string> { "Page1", "Page2" 
                }; 

      bool IsPageAvailbleInList = false; 

      foreach (string page in pageList) 
      { 

       if (this.Title.Equals(page)) 
       { 
        IsPageAvailbleInList = true; 
        break; 
       } 
      } 


      if (!IsPostBack && Session[this + "State"] != null) 
      { 

       if (IsPageAvailbleInList) 
       { 
        NameValueCollection formValues = (NameValueCollection)Session[this + "State"]; 

        String[] keysArray = formValues.AllKeys; 
        if (keysArray.Length > 0) 
        { 
         for (int i = 0; i < keysArray.Length; i++) 
         { 
          Control currentControl = new Control(); 
          currentControl = Page.FindControl(keysArray[i]); 
          if (currentControl != null) 
          { 
           if (currentControl.GetType() == typeof(System.Web.UI.WebControls.TextBox)) 
            ((TextBox)currentControl).Text = formValues[keysArray[i]]; 
           else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList)) 
            ((DropDownList)currentControl).SelectedValue = formValues[keysArray[i]].Trim(); 
           else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.CheckBox)) 
           { 
            if (formValues[keysArray[i]].Equals("on")) 
             ((CheckBox)currentControl).Checked = true; 
           } 
          } 
         } 
        } 
       } 
      } 
      if (Page.IsPostBack && IsPageAvailbleInList) 
      { 
       Session[this + "State"] = Request.Form; 
      } 
     } 
     catch (Exception ex) 
     { 
      LogHelper.PrintError(string.Format("Error occured while loading {0}", this), ex); 
      Master.ShowMessageBox(enMessageType.Error, ErrorMessage.GENERIC_MESSAGE); 

     } 
    } 
    }