2011-03-24 54 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); 

     } 
    } 
    }