Search code examples
asp.netpostbackviewstate

ASPX code behind ViewState not saving


I am trying to use ViewState to retain my textbox's input during a postback. Here's the code:

 private Label store_Incident_Number = new Label();
 private TextBox Incident_Number = new TextBox();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["Incident_Number"] != null)
        {                
            store_Incident_Number.Text = "TEST" + (string)ViewState["Incident_Number"];
            Panel1.Controls.Add(store_Incident_Number);
        }
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        ViewState.Add("Incident_Number", Incident_Number.Text);

    }  

above code located in my default.aspx.cs (code behind). The Panel1 is an asp control some where on the web page outside of a form tag. I am trying to follow this example: https://msdn.microsoft.com/en-us/library/ms227551(v=vs.85).aspx

I was hoping the Incident_Number textbox's text is saved onto the label after the page is postback, but it doesn't seems to work and I couldn't figure out the problem. Note that the textbox is dynamically added to the form of the page and is recreated every postback. My question is how I code to retain the textbox's value after the page is being postback?

EDIT: Fixed! Please see my accepted answer. Also, any eventhandler or other process that have potential to trigger another postback during Page_Init will refresh your value saved in viewstate (i.e. saved text is gone).


Solution

  • As I mentioned in my comment, you're not letting the WebForms system work for you. Here's an example of a form with dynamically created controls maintaining viewstate. Per your description, the panel is outside the form.

    When the page is displayed, you can enter new text and click submit repeatedly. The TextBox value will automatically be retained.

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        </form>
        <asp:Panel ID="Panel1" runat="server"></asp:Panel>
    </body>
    </html>
    

    Default.aspx.cs

    using System;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        private Label store_Incident_Number = new Label();
        private TextBox Incident_Number = new TextBox() { ID = "TextBox1" };
        private Button SubmitButton = new Button() { ID = "Button1", Text = "Submit" };
    
    
        protected void Page_Init(object sender, EventArgs e)
        {
            // what is created during this event has viewstate restored to it
            Panel1.Controls.Add(store_Incident_Number);
            form1.Controls.Add(Incident_Number);
            form1.Controls.Add(SubmitButton);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // the viewstate has already been restored so you can access the content of the TextBox
            store_Incident_Number.Text = Incident_Number.Text;
        }
    }