Question

I have a question regarding the scope of a class that I created in relation to a web application. My class is CCourseInfo and I create it as a private member of one of my web pages Enrollment. In the Page_Load method, there is a database call to load the table data in the class member, DataTable. This DataTable is bound to a gridview. This is the code:

 public partial class Enrollment : System.Web.UI.Page
{
   private Course CCourseInfo = new Course("Courses");
  protected void Page_Load(object sender, EventArgs e)
    {          
        try
        {
            if (!IsPostBack)
            {
                //Get the Environment Setting to determine the database to access
                txtBoxEnvironment.Text = CurrentEnvironment;
                DAL.setCurrentEnvironment(CurrentEnvironment);

                //Get Course information from database
                CCourseInfo.getData();
                 CourseGridView.DataSource = CCourseInfo.TableInfo;
                CourseGridView.DataBind();

            }
       }
}

I want to implement Paging. This method works but I have to get the data again to populate the DataTable in the CCourseInfo class.

  protected void CourseGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        CourseGridView.PageIndex = e.NewPageIndex;
        CCourseInfo.getData();
        CourseGridView.DataSource = CCourseInfo.TableInfo;
        CourseGridView.DataBind();
    }

My question is: Why do I have to get the data again? I set the DataTable in the Page_Load method and declared the class under the Enrollment class. Shouldn't the data still exists in the DataTable? How can I change my design so that I only have to get the data once?

Thanks,

Was it helpful?

Solution

You have to get the data again because ASP.NET (and in general, web pages) are stateless objects - this means that after each execution of your page, the page is cleaned up and all of its state is deleted. You'll have to recreate all of it when a new request comes in for the same page - the same page class will be instantiated again and you'll go through the page life cycle once more from beginning to end.

If you're not familiar with the page life cycle, here's an SO question / answers with quite a bit of detail - this will help you understand better what's going on during a request.

There are some things you can try (not an exhaustive list):

  • Cache all your records from the database in a cache server or in session state. If you have a lot of records, this could use a lot of memory (for each user) since all pages are loaded at once - you don't hit your database every time to query data. Caching speeds things up quite a bit but you may showing outdated data to the user - another user may change the data. It's also fairly tricky to properly handle cached data (when to remove it, when to fetch a fresh copy, etc.)

  • You can query only records for a single page - this results in lower data traffic between the web server and the database. You also have to query data every time the page changes, since you don't have more than the current page.

  • You can query all data and then send it to the client side and perform all paging in the browser. This generates a lot of traffic, since you have to send all pages to the client, whether the user wants to look at them or not. Paging is much faster this way but the browser uses a lot of memory and getting the data to the client can take a long time, depending on network speed.

The best solution is to get as little data as you can get away with (one page for the grid) - if you have to re-query it, the database will send you as little as needed.

OTHER TIPS

Ok first because Web is using HTTP which is a stateless protocol so that's why you loose a web page's state. So you have to manage the state your self. there are many ways that you can use to manage state.

Here is how i solved this problem i saved the data that i received from database and stored the entire list in View-state. And every time there is a post-back i retrieve the data from view-state.

            if (!Page.IsPostBack)
        {
            ViewState["data"] = MyDataset;
            DataListView.DataSource = ViewState["data"];
            DataListView.DataBind();
        }

        if (Page.IsPostBack)
        {
            DataListView.DataSource = ViewState["data"];
            DataListView.DataBind();
        }

you can also use Session state and a few other state management techniques.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top