Can I save the postback status and restore it?

Suppose after clicking on asp:button

I would like to save the actual "view" with the selected value for each controller, like asp: checkbox or input, inserted by users into the input field.

Then, I change the page with a link, for example "back to previous page". I would like to "restore" the old de facto form before leaving it.

Is this possible with .NET? Or do I need to implement all the controls in variables and put them in the session?

+3


source to share


2 answers


You don't need sessions for this. Just use JavaScript.

You have two pages Page1.aspx and Page2.aspx. You have text boxes, check boxes, radio buttons on your .aspx page, and you have a Submit button. Clicking the button will move it to the .aspx page.



Just click the Back to Previous Page button on page 2.aspx, which when clicked will take you to Page1.aspx with all these values โ€‹โ€‹that still exist. To do this, add this line to your page2.aspx page_load event

protected void Page_Load(object sender, EventArgs e)
{
 btnBackButton.Attributes.Add("onClick", "javascript:window.history.go(-1);return false;");
}

      

+1


source


There are several ways to store or pass values โ€‹โ€‹between ASP.NET pages.

Take a look here for more information.

Since you mentioned that you have "tons" of controls to store and recover, I tried to find a way to automate this process.

Here's my approach which uses Session as storage to store all the values โ€‹โ€‹of the type controls IPostBackDataHandler

. This is not actually tested, but hopefully helpful anyway.

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public class FormStorage
{
    private System.Web.UI.Page _page = null;

    public Dictionary<String, Dictionary<String, String>> storage
    {
        get { return (Dictionary<String, Dictionary<String, String>>)_page.Session["FormStorage"]; }
        set { _page.Session["FormStorage"] = value; }
    }

    public FormStorage(System.Web.UI.Page page)
    {
        _page = page;
        initHandler();
        if (this.storage == null)
        {
            this.storage = new Dictionary<String, Dictionary<String, String>>();
        }
        if(!this.storage.ContainsKey(_page.Request.Path))
            this.storage.Add(_page.Request.Path, new Dictionary<String, String>());
    }

    private void initHandler()
    {
        _page.Init += Init;
        _page.Load += Load;
    }

    private void Init(Object sender, EventArgs e)
    {
        loadForm();
    }

    private void Load(Object sender, EventArgs e)
    {
        saveForm();
    }

    private void loadForm()
    {
        var pageStorage = storage[_page.Request.Path];
        var e = pageStorage.GetEnumerator();
        while (e.MoveNext())
        {
            loadControl(e.Current.Key, e.Current.Value);
        }
    }

    private void loadControl(String ID, String value)
    {
        Control control = findControlRecursively(_page, ID);
        if (control != null)
        {
            setControlValue(control, value);
        }
    }

    private void setControlValue(Control control, String value)
    {
        if (control is ITextControl)
        {
            ITextControl txt = (ITextControl)control;
            txt.Text = value == null ? "" : value;
        }
        else if (control is ICheckBoxControl)
        {
            ICheckBoxControl chk = (ICheckBoxControl)control;
            chk.Checked = value != null;
        }
        else if (control is ListControl)
        {
            ListControl ddl = (ListControl)control;
            if (value == null)
                ddl.SelectedIndex = -1;
            else
                ddl.SelectedValue = value;
        }
    }

    public void saveForm()
    {
        saveControlRecursively(this._page);
    }

    private void saveControlRecursively(Control rootControl)
    {
        if (rootControl is IPostBackDataHandler)
        {
            var postBackData = _page.Request.Form[rootControl.ID];
            storage[_page.Request.Path][rootControl.ID] = postBackData;
        }

        if (rootControl.HasControls())
            foreach (Control subControl in rootControl.Controls)
                saveControlRecursively(subControl);
    }

    private static Control findControlRecursively(Control rootControl, String idToFind)
    {
        if (rootControl.ID == idToFind)
            return rootControl;

        foreach (Control subControl in rootControl.Controls)
        {
            Control controlToReturn = findControlRecursively(subControl, idToFind);
            if (controlToReturn != null)
            {
                return controlToReturn;
            }
        }
        return null;
    }
}

      

Here is an example implementation on a page that redirects to another page:



private FormStorage storage;

protected void Page_PreInit(object sender, EventArgs e)
{
    storage = new FormStorage(this);
}

protected void BtnRedirect_Click(object sender, EventArgs e)
{
    Response.Redirect("RedirectForm.aspx");
}

      

Note that it is loaded and saved implicitly on Page_Load/Page_PreRender

. Therefore, an instance FormStorage

must be created in Page_PreInit

.

If you want to programmatically change the values โ€‹โ€‹after Page_Load

, you need to call storage.saveForm()

manually (for example in Page_PreRender

) to make sure these values โ€‹โ€‹are overridden, as it FormStorage

will automatically save all postback data to Page_Load

.

Edit . The history.go

sh4nx0r approach is probably better as it is more scalable. My approach is using session and is not suitable for an internet application with a huge number of (possible) users.

One (slight) advantage of mine is that it will work even if javascript is disabled. Another advantage is that you can control which page you want to redirect to. You can even restore values โ€‹โ€‹for multiple redirects.

+3


source







All Articles