开发者

How to save an array in the ViewState and be able to retrieve it AFTER the page has unloaded?

Take the following code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        private List<Customer> CustomerList;
        protected void Page_Load(object sender, EventArgs e)
        {
            // Quickly Illustrate  Creation of a List of that contains information;
            //CustomerList = (!this.IsPostBack) ? new List<Customer>() : new List<Customer>((Customer[])ViewState["Customers"]);
            if (IsPostBack)
                CustomerList = new List<Customer>((Customer[])ViewState["Customers"]);
            else
                CustomerList = new List<Customer>();
            // Convert the List to Array of Customers and Save To View State
            // Rather than Handling Serialization Manually
            //ViewState.Add("Customers", CustomerList.ToArray());

            // While Reading the View State Information - Of course
            // use correct checks to see the item is Not Null and All that... and then do:
            //Customer[] newArray = (Customer[])ViewState["Customers"];
            //List<Customer> newList = new List<Customer>(newArray);
            //for (int i = 0; i < CustomerList.Count; i++)
            //    Response.Write(CustomerList[i].CustomerName + "\r\n");

        }

       protected void Page_Unload(object sender, EventArgs e)
        {
            for (int i = 0; i < CustomerList.Count; i++)
                Response.Write(CustomerList[i].CustomerName + "\r\n");
            ViewState.Add("Customers", CustomerList.ToArray());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Customer SingleCustomer = new Customer();
            SingleCustomer.CustomerName = TextBox1.Text;
            CustomerList.Add(SingleCustomer);
            ViewState.Add("Customers", CustomerList.ToArray());
        }
    }
}

It doesn't work. Every time I click the Add button and the page reloads, I get a NullReferenceException for "开发者_JAVA百科CustomerList = new List<Customer>((Customer[])ViewState["Customers"]);" because it's not there in the ViewState. Why is that?


Page Unload is simply too late to set a ViewState variable

From http://msdn.microsoft.com/en-us/library/ms178472.aspx:

The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

ViewState itself is sent as hidden fields in the page - so if you've already sent the page to the client, then you can't later add to the ViewState.

Try the LoadComplete or PreRender event instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜