C# GridView Customize Paging

4/1/2014·3 min read

GridView + Customized Paging

So, this's article is demostrate how to use cutomized paging in GridView. Following just example view of customized paging, you could use this as template to extend to what you want, but basically, you'll add your own control on server side to display. Following sample code is just the way I'm doing, you could totally change it to other way, and i know it's not good looking, i just typed in hurry. But anyway, first, it's working version for you to try it out.

 

so your aspx page just need GridView and its Pager Template

aspx page
  <asp:GridView  ID="GridView1"  runat="server"  AutoGenerateColumns="true"
 		 ondatabound="GridView1_DataBound"
 		 onpageindexchanging="GridView1_PageIndexChanging"  AllowPaging="True">
 		 <PagerTemplate>
 			 <asp:PlaceHolder  ID="PlaceHolder1"  runat="server"></asp:PlaceHolder>
 		 </PagerTemplate>
 	 </asp:GridView>
 

on your backend server side code, what i did was, before gridview databound event, add few controls to it to display customized paging. and i add event for textbox, so when textbox's text changed, let it to go to textbox changed event, to re-bind grid view's data to display to UI.

backed code
 protected  void  Page_Load(object  sender, EventArgs  e)
 		{
 			GridView1.DataSource = new  BusinessLogic ().GetProductData();
 			GridView1.DataBind();
 		}
 
 		protected  void  GridView1_PageIndexChanging(object  sender, GridViewPageEventArgs  e)
 		{
 			GridView1.PageIndex = e.NewPageIndex;
 			GridView1.DataSource = new  BusinessLogic ().GetProductData();
 			GridView1.DataBind();
 		}
 
 		protected  void  MyText_Changed(object  sender, EventArgs  e)
 		{
 			GridViewRow  row = GridView1.BottomPagerRow;
 
 			PlaceHolder  place = row.FindControl("PlaceHolder1" ) as  PlaceHolder ;
 			TextBox  textbox1 = place.FindControl("textbox1" ) as  TextBox ;
 
 			int  currentIndex;
 			int .TryParse(textbox1.Text, out  currentIndex);
 
 			place.Controls.Clear();
 
 			Label  label1 = new  Label ();
 			label1.Text = "Page " ;
 
 			TextBox  textbox = new  TextBox ();
 			textbox.Text = currentIndex.ToString();
 			textbox.TextChanged += new  EventHandler (MyText_Changed);
 
 			Label  label2 = new  Label ();
 			label2.Text = "of "  + GridView1.PageCount;
 
 			place.Controls.Add(label1);
 			place.Controls.Add(textbox);
 			place.Controls.Add(label2);
 
 			GridView1.PageIndex = currentIndex;
 			GridView1.DataSource = new  BusinessLogic ().GetProductData();
 			GridView1.DataBind();
 		}
 
 		protected  void  GridView1_DataBound(object  sender, EventArgs  e)
 		{
 			GridViewRow  row = GridView1.BottomPagerRow;
 
 			int  currentIndex = GridView1.PageIndex;
 			PlaceHolder  place = row.FindControl("PlaceHolder1" ) as  PlaceHolder ;
 			place.Controls.Clear();
 			Label  label1 = new  Label ();
 			label1.Text = "Page " ;
 
 			TextBox  textbox = new  TextBox ();
 			textbox.Text = currentIndex.ToString();
 			textbox.ID = "textbox1" ;
 			textbox.TextChanged += new  EventHandler (MyText_Changed);
 
 			Label  label2 = new  Label ();
 			label2.Text = "of "  + GridView1.PageCount;
 
 			place.Controls.Add(label1);
 			place.Controls.Add(textbox);
 			place.Controls.Add(label2);
 		}