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

后端 未结 4 1460
广开言路
广开言路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 16:55

    First off, do not use, as a general rule of thumb, static variables in an web application. These act as global variables and are not instantiated with each request.

    I wouldn't also suggest you using DataTables all the way up to your UI layer. Instead, work with strongly-typed objects.

    1. Make a Model of the object you are trying to bind.

    Like for example if you have a table called person that has the following fields.

    Id | first_name | last_name | audit_ts
    

    You can create an object as such:

    public class Person
    {
        public int Id {get;set;}
        public string FirstName {get;set;}
        public string LastName {get;set;}
    }
    
    1. Now in a separate functions, in some class you can call your stored procedure from the database and then cast your table rows in the person table into the list of Person Object.

    2. Now, instead of calling your stored procedure twice to get the same data, which only reduces your application's performance, what you can do is to instead of binding your grid view in your code behind at Page_Load event. Simply bind the HTML table after you make the call to your webmethod which I believe is in your code-behind. You can refer to this post regarding how to bind your HTML table with JSON object returned by your Ajax call.

    3. This way, you are making one call to the server and to the database to use the same data to bind your table as well as your charts.

提交回复
热议问题