简体   繁体   中英

asp.net/C# control - store value from previous page load

  • control (eg label) changes value when page loads.
  • before page load, label value is "x"
  • proceed to load page naturally x is lost at this point, so what is a simple way to store the value from the previous page load?

It sounds like what you want is a history of previous states of the page, or at least just one previous value. I can suggest a way to do this, but with a caveat: Don't overuse it. You can persist data between posts in ViewState but that data gets written to the page itself. That way when the users posts the form, they're also posting that data back. (More at the end.)

Here's a simple example. First, define some class that contains all of the additional state you want to store:

[Serializable]
public class PageState
{
    public string MyLabelPreviousText {get;set;}
}

Then in your code behind:

public partial class MainPage : System.Web.UI.Page
{
    private PageState _pageState;

    protected void Page_Load(object source, EventArgs e)
    {
        _pageState = ViewState["pageState"] as PageState ?? new PageState();
        _pageState.MyLabelPreviousText = MyLabel.Text;
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["pageState"] = _pageState;
    }

When you load the page, you're checking to see if you've already saved your class (in this case called PageState ) to the ViewState . If you haven't, you create a new one.

In the PreRender event, after you're done updating the page, you're updating that class with the text of your label and then saving the whole thing to ViewState again. That way the next time the page loads you can retrieve it again.

I'm not sure at which point you want to save the previous text of your label. In this example it's saving the text during the Load event. That way if the text is changed at any point from then on, the value you have saved is the original text of the label. The details may vary depending on what you're trying to do, but this pattern lets you save that sort of custom data without using Session and piling up data like this in memory.

I mentioned not overusing it. If you save some labels, controls, and other data, ViewState probably won't get too large. ASP.NET is already putting the state of every server control there anyway. But if you go really crazy with it then ViewState can get huge. All of that data gets written to the page, and all of it gets posted back to the server with each postback.

It's probably not a huge concern, but be mindful of it. Use Chrome dev tools, Fiddler, or even just inspect your page source to see if that data is getting so large that it might impact performance.

For anyone who isn't familiar, ViewState looks like this in the HTML source:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
     value="/wEPDwULLTEyMzgxNzgyNDIPZBYCZg9kFgICAQ9kFggCAQ9kFgJmD2QWAgIBD2QWAmYPZBYMA
... etc.

As you can see, it's actually a hidden form input. ASP.NET is placing all of the details of the page's controls in the form so that when you post the form, it can piece all of that together and make sure that the page looks the same after postbacks. That's the reason why when we set up a lot of controls the first time we check if(!IsPostBack()) . If it is a postback then we don't need to populate the dropdowns, etc. all over again. They are restored from that ViewState data.

This approach is functionally the same as adding an additional hidden input as suggested in one comment. The difference is that you're using an existing hidden input (viewstate) and if you need to save multiple values you're just making them properties of a class instead of adding more and more hidden inputs.

I'm not sure if this is what you mean without any code samples but I know from memory that in ASP.NET you can access the previous page property like so:

Page lastPage = currentPage.PreviousPage

Which returns the entire Page object. Assuming your label is defined like so:

<asp:label id="myLabel" runat="server" />

Then you can access the text property with:

Label myLabel = lastPage.FindControl("myLabel") as Label;
lastPageVal = myLabel.Text

So ensure that lastPageVal is a static variable, then it will also persist throughout pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM