3

i have gridview with lots of records, so had to use ObjectDataSource so as to fetch only the records that will be displayed on the page i.e. if there are total 100 records, and page size is 10, on each page click only 10 records are fetched from DB. Please find the code below

///aspx

<asp:GridView ID="grdModulesList" CssClass="moduleList" runat="server"
    AutoGenerateColumns="False" HeaderStyle-Font-Size="Smaller"
    Font-Size="Small" AllowPaging="true" AllowSorting="True" OnRowCreated="grdModulesList_RowCreated"
    OnRowDataBound="grdModulesList_RowDataBound" BackColor="White" BorderColor="Blue"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize="20">
    <Columns>
        <asp:BoundField DataField="collection_id" Visible="False" />
        <asp:BoundField HeaderText="Item" />
        <asp:BoundField HeaderText="Module Name" DataField="module_name" SortExpression="module_name"
            ControlStyle-CssClass="moduleName" />
        <asp:BoundField HeaderText="File Name" DataField="module_file_name" SortExpression="module_file_name" />
        <asp:BoundField HeaderText="ID" DataField="defect_number" SortExpression="defect_number" />            
        <asp:BoundField HeaderText="Actions" />
    </Columns>
    <RowStyle BorderColor="Blue" BorderStyle="Solid" CssClass="grid_width" BorderWidth="2px"
        Height="20px" />
    <PagerStyle BackColor="#FFFF99" ForeColor="Red" HorizontalAlign="Center" Height="20px" />
    <FooterStyle ForeColor="black" Font-Bold="true" />
    <SelectedRowStyle Font-Size="Smaller" />
    <HeaderStyle Font-Size="Smaller" BorderStyle="Solid" BackColor="Gold" Font-Bold="True"
        ForeColor="Black" Height="30px" />
    <AlternatingRowStyle BorderColor="#3366FF" BorderStyle="Solid" BorderWidth="1px" />
</asp:GridView>

//.aspx.cs

        ObjectDataSource ods = new ObjectDataSource();
        ods.ID = "ods"; 
        ods.SelectMethod = "GRAD_ModuleListforCollection_Subset";  //method to fetch records from DB
        ods.EnablePaging = true;
        ods.TypeName = "pmAdmin.classes.data.ApplicationData";
        ods.StartRowIndexParameterName = "StartRecord";
        ods.MaximumRowsParameterName = "PageSize";
        ods.SortParameterName = "SortBy";
        ods.SelectCountMethod = "GRAD_Total_Modules";
        Parameter p1 = new Parameter("userID", TypeCode.String, userId);
        ods.SelectParameters.Add(p1);
        panelModuleList.Controls.Add(ods);  //add objectDatasource control to a panel

        grdModulesList.DataSourceID = ods.ID;
        grdModulesList.DataBind();

//method GRAD_ModuleListforCollection_Subset

public System.Data.DataSet GRAD_ModuleListforCollection_Subset(string userID, int StartRecord, int PageSize, string SortBy)
                 {}

records are correctly binded to Gridview, but the issue is the method used to fetch the records from DB i.e.GRAD_ModuleListforCollection_Subset is called twice for each click on page. example : if i click on page 2 the method is called () first time with startRecord=0,pagesize=20 () called second time with startRecord=20,pagesize=20

after click on page 2 if i click page 3 () first time with startRecord=20,pagesize=20 () called second time with startRecord=40,pagesize=20

for every page click, first load is having previous value.

Kindly help me in resolving this.

Thanks in advance.

user166013
  • 1,411
  • 4
  • 21
  • 37

2 Answers2

0

made the following changes to make it work

  1. enable caching for objectDatasource
    ods.EnableCaching = true;

  2. set Session["sortorder"]

    grdModulesList.DataSourceID = ods.ID;
    grdModulesList.DataBind();
    if (Session["sortorder"] == null)
        Session["sortorder"] = "Ascending";
    
  3. add gridview sorting event

    protected void grdModulesList_Sorting(object sender, GridViewSortEventArgs e) { ods.SelectParameters["SortBy"].DefaultValue = GetSortExpr(e.SortExpression); e.Cancel = true; //We have to do this or we will get an exception }

    public string GetSortExpr(string sortExp)
    {
    
    if (Session["sortorder"].ToString() == "Ascending")
    {
        Session["sortorder"] = "Descending";
        return sortExp + " DESC";
    }
    else
    {
        Session["sortorder"] = "Ascending";
        return sortExp + " ASC";
    }
    }
    
user166013
  • 1,411
  • 4
  • 21
  • 37
0

if you declare the ObjectDataSource and all its setting in Markup portion (.aspx), then the Select method is called twice only when page is requested for very first time ( PageIndex defaults to 1).

when you select page 2 from paging links, it will call only once the select method then the SelectCount method.

R.C
  • 10,417
  • 2
  • 35
  • 48