For the motivation for this article see Retain "Number of Records" to display in GridView by defyant_2004 where the idea is, if I change the the pager size in the list page go to another page and then return to the ListPage chances are I still want the pager size to be the same.
So here the simple solution:
protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e) { if (_gridView == null) return; DropDownList dropdownlistpagersize = (DropDownList)sender; _gridView.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue); // save pager size if (Session["DD_PagerSize"] != null) Session["DD_PagerSize"] = _gridView.PageSize.ToString(); else Session.Add("DD_PagerSize", _gridView.PageSize.ToString()); int pageindex = _gridView.PageIndex; _gridView.DataBind(); if (_gridView.PageIndex != pageindex) { //if page index changed it means the previous page was not valid and was adjusted. Rebind to fill control with adjusted page _gridView.DataBind(); } }
Listing 1 – the DropDownListPageSize_SelectedIndexChanged event for the GridViewPager
protected void Page_Load(object sender, EventArgs e) { Control c = Parent; while (c != null) { if (c is GridView) { _gridView = (GridView)c; // if pager size saved then restore it var pagerSize = Session["DD_PagerSize"]; if (_gridView != null && pagerSize != null) { _gridView.PageSize = Convert.ToInt32(pagerSize); } break; } c = c.Parent; } }
Listing 2 – the Page_Load event for the GridViewPager
Listings 1 & 2 are the minimum we need to do to get this functionality see the Forum post for a VB example.
But my thoughts turn to what if I want a different setting for different tables?
So here it is:
Add a private variable to the user control
public partial class GridViewPager : System.Web.UI.UserControl { private GridView _gridView; private MetaTable _table;
Listing 3 – global _table variable
protected void Page_Load(object sender, EventArgs e) { Control c = Parent; while (c != null) { if (c is GridView) { _gridView = (GridView)c; // if pager size saved then restore it var GridDataSource = _gridView.FindDataSourceControl(); _table = GridDataSource.GetTable(); var pagerSize = Session[_table.Name + "_PagerSize"]; if (_gridView != null && pagerSize != null) { _gridView.PageSize = Convert.ToInt32(pagerSize); } break; } c = c.Parent; } }
Listing 4 – Page_Load
In Listing 4 we get the GridViews DataSource and then find out which table we are working on, then we create a session variable to hold the pager size for this table by combining the table name with “_PagerSize” string.
protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e) { if (_gridView == null) return; DropDownList dropdownlistpagersize = (DropDownList)sender; _gridView.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue); // save pager size if (Session[_table.Name + "_PagerSize"] != null) Session[_table.Name + "_PagerSize"] = _gridView.PageSize.ToString(); else Session.Add(_table.Name + "_PagerSize", _gridView.PageSize.ToString()); int pageindex = _gridView.PageIndex; _gridView.DataBind(); if (_gridView.PageIndex != pageindex) { // if page index changed it means the previous page was page // not valid and was adjusted. Rebind to fill control with adjusted _gridView.DataBind(); } }
Listing 5 - DropDownListPageSize_SelectedIndexChanged
Now in Listing 5 we can retrieve the pager size based on table name.
1 comment:
Nice !
Can't wait to use this example in my current project :)
Regards,
-Yannick86
Post a Comment