Search code examples
asp.netjquerylifecycle

what is the lifespan of a datatable after an ajax call?


I'm doing a jquery ajax call to my server side c#. The c# code fills a datatable then sends the data to the aspx page using a JavaScriptSerializer: The datatable is initialized as a public:

public partial class _Default : System.Web.UI.Page
   {
      DataTable myDataTable = new DataTable();

      protected void Page_Load(object sender, EventArgs e)
         {
            if (!Page.IsPostBack)
            {
               //SqlDataReader builds loads data into dataTable...
               HttpContext.Current.Response.Write(serializer(dataTable));
            }

The aspx page finally creates a table from the data by posting it back to the aspx page. My next task is to sort the rows ascending/descending when the table heading is clicked (using another ajax call).

I'd like to perform a sort operation similar to the accepted answer in this question: Sorting rows in a data table

Will the datatable from the initial ajax call still be in memory? Thank you.


Solution

  • No, the dataTable will not be in memoty anymore. Every time you create a request a new instance of _Default will be created. After the request has been handled, it will be destroyed.

    If you want the DataTable to remain in memory, you should save it in the Cache or the Application object (HttpContext.Current.Cache / HttpContext.Current.Application).