Cycling between view states in .net?

101 Views Asked by At

I've currently got code that displays a string showing that another view state is being activated. How would I go about adding another button that will cycle back through to the previous state, NOT just hiding the string (though effectively that's what it is). Eventually I want to be able to run through multiple methods using view state (including a fileupload and displaying data from a database / data from the document)

My code is as such;

<script runat="server"> 
   protected void Page_Load(object sender, EventArgs e)
   {
        if(!IsPostBack)
        {
            string viewStateDisplay = "ViewState 2 is now being displayed";
            if(ViewState["PageState"]==null)
            {
                ViewState["PageState"] = viewStateDisplay;
            }    
         }
   }    
   protected void btnClick_Click(object sender, EventArgs e)
   {
         lblString.Text = ViewState["PageState"].ToString();
   }    
</script>

<div>
    ViewState Data: <b><asp:Label ID="lblString" runat="server"/></b>
    <asp:Button ID="btnClick" runat="server" Text="Get ViewState Data"
         onclick="btnClick_Click"/>
</div>
1

There are 1 best solutions below

0
On

Added another button and edited the other one so it looks like this. This can be used to test my methods that I will place inside and give viewstate numbers and close them according to their number, the button now gives me a way to test this is working.

 protected void btnClick_Click(object sender, EventArgs e)
   {
    lblString.Text = ViewState["PageState"].ToString();
    lblString.Visible = true;

   }

   private void Close(object sender, EventArgs e)
   {
       lblString.Visible = !lblString.Visible;
       lblString.Visible = false;
   }

ViewState Data: <b><asp:Label ID="lblString" runat="server"/></b>
<asp:Button ID="btnClick" runat="server" Text="Get ViewState Data" OnClick="btnClick_Click"/>
<asp:Button ID="Closeform" runat="server" Text ="Hide PageState" OnClick="Close" />