Can we use same datatable for pagemethod and webmethod in ASP.NET?

后端 未结 4 1368
广开言路
广开言路 2021-02-20 16:02

I am trying to create a new webpage where i need to display almost 10 different gridviews and charts.

Gridviews are binded on pageload event and charts are displayed usi

4条回答
  •  鱼传尺愫
    2021-02-20 16:49

    This is a good use case for the little used Cache Object Many users understand ViewState and SessionState, however the Cache object is not as widely utilized, and although the concept is very similar, it is much more flexible.

    If your page is calling 10 stored procedures twice (once for your grids and a second time for your charts) then lets improve the performance by roughly 100% by eliminating the extra calls with the Cache Object

    Have one call to the stored procedures in a separate method that populate your data tables cache object, which is then reused throughout your application.

    private void loadReport1IntoCache()
    {
      //...load your data from DB into the Report1 variable here
    
    
      //this line is new, and it saves your data into a global Cache variable
      //with an absolute expiration of 10 minutes
      Cache.Insert("Report1", Report1, null,
      DateTime.Now.AddMinutes(10d), 
      System.Web.Caching.Cache.NoSlidingExpiration);
    
    
    }
    

    Then, when you are inside your other methods, you can use the Cache variable instead of calling stored procedures again. For example:

    [System.Web.Services.WebMethod]
    public static string GetDataReport1()
    {
       //first load the application variable before performing your other work
       DataTable myCachedReport1Data = (DataTable)Cache["Report1"];
       //did the Cache expire?
       if (myCachedReport1Data == null)
       {
       //if so refresh it
       loadReport1IntoCache();
       //and then assign the variable the contents of the refresh and proceed
       myCachedReport1Data = (DataTable)Cache["Report1"];
       }
    
       //other work here, utilizing the myCachedReport1Data variable
    }
    

    and for your grid binding:

    private void gvbindReport1()
    {
        try
        {            
            DataTable myCachedReport1Data = (DataTable)Cache["Report1"];
            //did the Cache expire?
            if (myCachedReport1Data == null)
            {
              //if so refresh it
              loadReport1IntoCache();
              //and then assign the variable the contents of the refresh
              myCachedReport1Data = (DataTable)Cache["Report1"];
            }
    
            GdReport.DataSource = myCachedReport1Data ;
            GdReport.DataBind();
        }
        catch (Exception ex)
        {
            Log.Errlog("Error Occured in  gvbindReport1 : " +  ex.Message.ToString());
        }
    
    }
    

    Now, you will have to do a few things not mentioned here. You should consider when you want your Cache data to expire (the example given is 10 minutes). Also you should consider if you want it to be an Absolute Number of minutes (Absolute Expiry) or a number of minutes since last access (Sliding Expiry). In your case, probably absolute expiry, but only you know that. Then you will set the expiration when you are setting the variable contents.

    See the Cache documentation here: https://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx

    Adding Cache data: https://msdn.microsoft.com/en-us/library/18c1wd61.aspx

    Retrieving Cache data: https://msdn.microsoft.com/en-us/library/xhy3h9f9.aspx

提交回复
热议问题