GridView + ObjectDataSource SelectMethod 被调用两次

3
我有一个包含许多记录的GridView,所以必须使用ObjectDataSource来获取只显示在页面上的记录,即如果总共有100条记录,每页大小为10,则在每个页面单击时仅从数据库获取10条记录。请参阅以下代码
///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)
                 {}

记录已正确绑定到Gridview,但问题在于从DB获取记录的方法即GRAD_ModuleListforCollection_Subset在每次点击页面时会被调用两次。 例如: 如果我点击第二页,该方法将 () 以startRecord=0,pagesize=20的参数第一次调用 () 以startRecord=20,pagesize=20的参数第二次调用

当我点击第二页后再点击第三页时, () 以startRecord=20,pagesize=20的参数第一次调用 () 以startRecord=40,pagesize=20的参数第二次调用

每个页面单击都会有先前值的第一个加载。

请帮助我解决这个问题。

提前致谢。

2个回答

0

进行了以下更改以使其正常工作

  1. 启用对象数据源的缓存
    ods.EnableCaching = true;

  2. 设置 Session["sortorder"]

    grdModulesList.DataSourceID = ods.ID;
    grdModulesList.DataBind();
    if (Session["sortorder"] == null)
        Session["sortorder"] = "Ascending";
    
  3. 添加 GridView 排序事件

    protected void grdModulesList_Sorting(object sender, GridViewSortEventArgs e) { ods.SelectParameters["SortBy"].DefaultValue = GetSortExpr(e.SortExpression); e.Cancel = true; //必须这样做,否则会出现异常 }

    public string GetSortExpr(string sortExp)
    {
    
    if (Session["sortorder"].ToString() == "Ascending")
    {
        Session["sortorder"] = "Descending";
        return sortExp + " DESC";
    }
    else
    {
        Session["sortorder"] = "Ascending";
        return sortExp + " ASC";
    }
    }
    

0
如果您在标记部分(.aspx文件)中声明了及其所有设置,则仅当第一次请求页面时(PageIndex默认为1),才会调用Select方法两次。
当您从分页链接选择第2页时,它将仅调用一次select方法,然后是SelectCount方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接