0

I need a way to save and load the Page State in a persistent manner (Session). The Project i need this for is an Intranet Web Application which has several Configuration Pages and some of them need a Confirmation if they are about to be saved. The Confirmation Page has to be a seperate Page. The use of JavaScript is not possible due to limitations i am bound to. This is what i could come up with so far:

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

I can't seem to find a way to load the Page State after being redirected from the Confirmation.aspx to the PageToConfirm.aspx, can anyone help me out on this one?

haze4real
  • 728
  • 1
  • 9
  • 19

2 Answers2

1

If you mean view state, try using Server.Transfer instead of Response.Redirect.

If you set the preserveForm parameter to true, the target page will be able to access the view state of the previous page by using the PreviousPage property.

Druid
  • 6,423
  • 4
  • 41
  • 56
  • I don't need the view state in the Confirmation.aspx i need the view state if i return to the PageToConfirm.aspx. – haze4real Mar 24 '11 at 10:03
  • Can you make a sample how to persist the Page State using Server.Transfer? Flow should be like this: Page1.aspx -> Page2.aspx -> Page1.aspx. The State of Page1.aspx should be the same as before transfering to Page2.aspx, e.g. If you have put something into a Textbox on Page1.aspx it should be there after you have been on Page2.aspx – haze4real Mar 24 '11 at 10:16
0

use this code this works fine form me

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);

        }
    }
    }
Muhammad Essa
  • 142
  • 1
  • 10