ASP.NET:如何在页面中保持页面状态?

时间:2011-03-24 09:49:24

标签: asp.net page-state

我需要一种以持久方式保存和加载页面状态的方法(会话)。我需要的项目是一个Intranet Web应用程序,它有几个配置页面,如果要保存它们,其中一些需要确认。确认页面必须是一个单独的页面。由于我受到限制,因此无法使用JavaScript。这是我到目前为止所能提出的:

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后,我似乎无法找到加载页面状态的方法,有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

如果您指的是查看状态,请尝试使用Server.Transfer代替Response.Redirect

  

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

答案 1 :(得分: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);

        }
    }
    }