2013-07-29 31 views
3

我有很多記錄的gridview,所以不得不使用ObjectDataSource,以便只提取將顯示在頁面上的記錄,即如果總共100條記錄,並且頁面大小爲10,在每個頁面上單擊只從數據庫中提取10條記錄。 請在下面找到gridview + objectdatasource selectmethod調用兩次

/// 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(); 

代碼//方法GRAD_ModuleListforCollection_Subset

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

記錄正確綁定到Gridview,但問題是用於從DB獲取記錄的方法,即GRAD_ModuleListforCollection_Subset在頁面上每次單擊時調用兩次。 例如:如果I 2頁上的單擊方法稱爲 ()第一次與startRecord = 0,頁大小= 20 ()稱爲第二次用startRecord = 20,頁大小= 20

點擊頁面上後2如果我點擊頁面3 ()第一次與startRecord = 20,頁大小= 20 ()稱爲第二次用startRecord = 40,頁大小= 20

的每一頁點擊,第一負載由具有先前的值。

請幫我解決這個問題。

在此先感謝。

回答

0

做了以下修改,使其工作

  1. 啓用緩存爲ObjectDataSource控件
    ods.EnableCaching = TRUE;

  2. 設置會話[ 「排序順序」]

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

    保護無效grdModulesList_Sorting(對象發件人,GridViewSortEventArgs E) { ods.SelectParameters [ 「SortBy」]。 DefaultValue = GetSortExpr(e.SortExpression); e.Cancel = true; //我們必須這樣做,或者如果你聲明的ObjectDataSource及其標記部分的所有設置,我們將得到一個異常 }

    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)。

當您從尋呼鏈接中選擇頁面2時,它將只調用一次select方法,然後調用SelectCount方法。

相關問題