asp.netasp.net-mvcpaginationasp.net-ajax

Paging results in asp.net


I am trying to find a solution in asp.net in order to achieve paging of data so 1) Application does not have to connect to DB on each page index change, in order to retrieve either entire set or just a paged set of results 2) Do not want to store results in Viewstate or Session

For large result sets, I was thinking of storing key values (ID, rownumber) in a result set (array, json) and then populate additional row information on row binding.

Is there a way to do it in asp.net, or I need to consider jQuery for this scenario.


Solution

  • 1) Application does not have to connect to DB on each page index change, in order to retrieve either entire set or just a paged set of results

    You need to cache your results. For example:

    Example:

    Caching Approach 1

        <asp:SqlDataSource runat="server" ID="sdsd"
            SelectCommand="select * from jobs"
            SelectCommandType="Text"
            DataSourceMode="DataSet"
            ConnectionString="<%$ConnectionStrings: PUBSConnectionString %>"
            CacheDuration="30"
            EnableCaching="true"
        >
        </asp:SqlDataSource>
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" DataSourceID="sdsd" DataTextField="job_desc" DataValueField="job_id">
        </asp:DropDownList>
    

    Caching Approach 2

    You could always cache your results in code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            IEnumerable<employee> res = Enumerable.Empty<employee>();
    
            if (this.Cache["emp"] == null)
            {
                res = this.GetEmployeesFromDatabase();
                this.Cache["emp"] = res;
            }
            else
            {
                res = this.GetEmployeesFromCache();
            }
    
            this.grv.DataSource = res;
            this.grv.DataBind();
        }
    }
    
    protected IEnumerable<employee> GetEmployeesFromDatabase()
    {
        // go to your database to get the objects
        return Enumerable.Empty<employee>();
    }
    
    protected IEnumerable<employee> GetEmployeesFromCache()
    {
        return this.Cache["emp"] as IEnumerable<employee>;
    }
    

    Keep in mind that the returned object must to be a DataSet or DataTable in order to add sorting capabilities to your cache results

    2) Do not want to store results in Viewstate or Session

    Again you need to use cache

    Is there a way to do it in asp.net, or I need to consider jQuery for this scenario.

    I don't see how jQuery (by itself) can help you with your requirement, I mean if you use AJAX posts, you would improve the perceived performance of your current page, but that won't help you with the connections to the database unless you use caching