0

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

确认请求:

[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;
    }
}

确认响应:

[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 }

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

4

2 回答 2

1

如果您的意思是视图状态,请尝试使用Server.Transfer而不是Response.Redirect.

如果将该preserveForm参数设置为true,则目标页面将能够通过该 PreviousPage属性访问上一页的视图状态。

于 2011-03-24T09:58:29.093 回答
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);

        }
    }
    }
于 2014-06-16T08:42:17.023 回答