2009-10-07 72 views
0

我用一個LinqDataSource標準的GridView。用戶可以使用庫存標準的東西對網格進行排序和翻頁。搜索條件(WhereParameters)也可用於過濾結果。這樣做效果很好,但是每當離開頁面時,狀態顯然都會丟失。ASP.NET GridView與LinqDataSource:記住排序,分頁和WhereParameters狀態

因此,捕獲Sort和Pagining狀態以及WhereParameter值的通用機制將非常棒。然後,用戶可以將這些值添加到會話中,並在用戶導航回頁面時恢復它們。

任何幫助將不勝感激。

我的代碼如下:

<asp:GridView ID="dataGridView" runat="server" 
     AllowPaging="True" 
     AllowSorting="True" 
     AutoGenerateColumns="False" 
     CssClass="GridView" 
     DataSourceID="linqDataSource" 
     PageSize="20"> 
    <Columns> 
     <asp:BoundField 
      HeaderText="Name" 
      DataField="Name" 
      SortExpression="Name" /> 
     <asp:TemplateField 
      HeaderText="Province" 
      SortExpression="Province.Name"> 
      <ItemTemplate> 
       <%# Eval("Province.Name")%></ItemTemplate> 
      </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
<asp:LinqDataSource ID="linqDataSource" runat="server" 
    ContextTypeName="DataContext" 
    TableName="Schools" 
    EnableUpdate="True" 
    EnableDelete="True" 
    Where="Name.Contains(@Name) && (ProvinceID == @ProvinceID)"> 
    <WhereParameters> 
     <asp:ControlParameter 
      Name="Name" 
      DefaultValue="" 
      ControlID="tbName" 
      Type="String" 
      ConvertEmptyStringToNull="False" /> 
     <asp:ControlParameter 
      Name="ProvinceID" 
      DefaultValue="" 
      ControlID="ddlProvince" 
      Type="Int32" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
+0

是你的gridview在updatepanel中嗎? – 2009-10-07 04:11:34

+0

是的。我已經排除了部分代碼。代碼起作用。只是當用戶返回頁面時,我想恢復「頁面視圖」。 – 2009-10-07 09:27:29

回答

0

安迪,

你有沒有考慮鉤住GridView的OnDataBound事件和在cookie中存儲適當的gridview的屬性?然後,您可以在Non PostBack PageLoad事件中點擊該cookie以將gridview恢復到用戶離開它的大約*。

  • 大概是因爲gridview可能有點不同,基於源數據上的任何CRUD操作在用戶離開時從其他活動發生。

如果您希望頁面長時間記住其狀態,則可以使用cookie過期屬性。

無論如何...這裏是一些示例代碼。

protected void gvCountryList_DataBound(object sender, EventArgs e) 
{ 
    // Store gvCountryList Gridview Sort, PageIndex, and PageSize info 
    // in a cookie so that we can redisplay same grid position next 
    // time they visit page. 
    HttpCookie aCookie = new HttpCookie("MaintainCountries"); 
    aCookie["SortExp"] = gvCountryList.SortExpression.ToString(); 
    aCookie["SortDir"] = gvCountryList.SortDirection.ToString(); 
    aCookie["PageIndex"] = gvCountryList.PageIndex.ToString(); 
    Response.Cookies.Add("aCookie"); 
) 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Page.IsPostBack == false) 
    { 

     //Grab the cookies 
     if (Request.Cookies["MaintainCountries"] != null) 
     { 
      if ((Request.Cookies["MaintainCountries"]["SortExp"] != null) 
      && (Request.Cookies["MaintainCountries"]["SortDir"] != null) 
      && (Request.Cookies["MaintainCountries"]["PageIndex"] != null)) 
      { 
       SortDirection srtDir = SortDirection.Ascending; 
       if (Request.Cookies["MaintainCountries"]["SortDir"].ToUpper() == "DESCENDING") 
        srtDir = SortDirection.Descending; 
       gvCountryList.Sort(Request.Cookies["MaintainCountries"]["SortExp"].ToString(), srtDir); 
       gvCountryList.PageIndex = Convert.ToInt32(Request.Cookies["MaintainCountries"]["PageIndex"].ToString()); 
      } 
     } 
    } 
} 

也許有比餅乾更好的方法。

我歡迎並批評社區反饋。我認爲自己是一個.net新手,所以如果任何人看到潛在的問題或危險,請提出你的想法。

Mike

+0

看起來很不錯!感謝你的回答。現在有點淹沒,但我很快就會嘗試並回復你。 – 2010-01-14 11:36:37

0

嗯,醜陋的方式是使用會話,並給它要麼MAJIC字符串或定義某處的價值爲他們

Session["_page"] = _currentPage; 
Session["_sortColumn"] = _currentSortColumn; 
Session["_sortDir"] = _currentSortDir; 

或者你可以變得棘手,並將其粘貼在用戶配置文件中。然後將這些值加載到page_load或類似的東西上。缺點是,如果有人航行,半小時後回來,他們又回到了他們最後的頁面等等。如果您正在尋找它們來導航,就像一個細節表單,然後返回,只需將這些值傳遞到下一頁。然後你可以返回查詢字符串的值或類似的東西。

享受。

+0

感謝您的回答,但不是真的我以後。我需要一些通用的方法來連接OnSorting和OnPageIndexChanging方法來保存排序和分頁信息。我還需要一種方法來存儲WhereParameters。當用戶返回頁面時,我需要一種方法來恢復這些保存的值。 – 2009-10-07 09:30:47