Sign in | JoinGridView DropDownList PagerThis post shows you how to add a custom DropDownlist pager and pager buttons to t...
Finally, add the following code to your common class (PresentationUtils in my case)public static void SetPagerButtonStates...
under div panel, [we have 20+ columns] any static header solutions screws up. Also the samecase for Atlas update panel ? T...
information because until i tried lots of things and the sorting is not handled until i use thedelegate{....} to simplify ...
Monday, January 29, 2007 12:27 AM by plibertine# re: GridView DropDownList PagerPls send this code in C#. PageIndex always...
Protected Sub setIndex(ByVal sender As Object, ByVal e As EventArgs)    SearchResultsGrid.PageIndex = sender.SelectedIndex...
btnNext.Enabled = True     Else        btnLast.Enabled = False        btnNext.Enabled = False     End If     lbltotal.Text...
how would i do if i want to have paging something like this:1 2 3 4 5 6 7 .. instead of dropdownlist?thanksSaturday, Augus...
int halfButtonCount = Convert.ToInt32(pageButtonCount / 2);if (pageIndex > halfButtonCount){startingPage = pageIndex - hal...
{LinkButton pageLink = new LinkButton();placeholderPageSelector.Controls.Add(pageLink);pageLink.Text = String.Format("{0}"...
Wednesday, August 29, 2007 10:36 AM by Steve O# re: GridView DropDownList PagerSteve,Thanks for your help.where do you add...
I need to find out the current row of gridview while the items in the dropdownlist in that row getchanged.Now work fine un...
when i use the vb code i got a syntax error saying that Name Utils not decalred eventhough i hvimports system.web.utils......
# re: GridView DropDownList Pagermany thansk it was so helpfull for meSaturday, April 12, 2008 7:20 PM by ar# re: GridView...
# re: GridView DropDownList PagerHi, i use this gridview dropdownlist pager in gridview1. then i use again in gridview2 in...
from the last line of the SetPagerButtonStates sub as this would obviously have the gridViewsselected index match that of ...
Upcoming SlideShare
Loading in …5
×

Sign in

1,191 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,191
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sign in

  1. 1. Sign in | JoinGridView DropDownList PagerThis post shows you how to add a custom DropDownlist pager and pager buttons to theGridView as shown below:Unlike the behavior of the default pager buttons, these buttons get disabled instead of beinghidden (depending on the state of the GridView).IMHO, buttons that remain at the same position while paging through a resultset makes a betterUI (spatial memory).The code is pretty straightforward.Add the following pager template to your GridView.<PagerTemplate>Goto Page <asp:DropDownList ID="ddlPageSelector" runat="server" AutoPostBack="true"> </asp:DropDownList> <asp:Button Text="First" CommandName="Page" CommandArgument="First" runat="server" ID="btnFirst" /> <asp:Button Text="Previous" CommandName="Page" CommandArgument="Prev" runat="server" ID="btnPrevious" /> <asp:Button Text="Next" CommandName="Page" CommandArgument="Next" runat="server" ID="btnNext" /> <asp:Button Text="Last" CommandName="Page" CommandArgument="Last" runat="server" ID="btnLast" /></PagerTemplate>Add a RowCreated event handler to your GridView and add the following code:protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Pager) { PresentationUtils.SetPagerButtonStates(GridView1, e.Row, this); }}
  2. 2. Finally, add the following code to your common class (PresentationUtils in my case)public static void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page) { int pageIndex = gridView.PageIndex; int pageCount = gridView.PageCount; Button btnFirst = (Button)gvPagerRow.FindControl("btnFirst"); Button btnPrevious = (Button)gvPagerRow.FindControl("btnPrevious"); Button btnNext = (Button)gvPagerRow.FindControl("btnNext"); Button btnLast = (Button)gvPagerRow.FindControl("btnLast"); btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0); btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount - 1)); DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl("ddlPageSelector"); ddlPageSelector.Items.Clear(); for (int i = 1; i <= gridView.PageCount; i++) { ddlPageSelector.Items.Add(i.ToString()); } ddlPageSelector.SelectedIndex = pageIndex; //Anonymous method (see another way to do this at the bottom) ddlPageSelector.SelectedIndexChanged += delegate { gridView.PageIndex = ddlPageSelector.SelectedIndex; gridView.DataBind(); };}AFAIK, VB.net 2.0 does not support anonymous methods. Therefore, for those of youconverting this code to VB.net, you will need to add the handler manually. Go to the GridViewTasks and select "Edit templates", select the PagerTemplate and double click on theDropDownList to add a index changed event handler. In the event handler, write the VB.netequivalent of this:protected void ddlPageSelector_SelectedIndexChanged(object sender, EventArgse) { GridView1.PageIndex = ((DropDownList)sender).SelectedIndex; GridView1.DataBind();}Related : Clickable GridView Headers .Published Monday, August 14, 2006 10:18 AM by rajbkFiled under: .NET, Data, ASP.NET, TipsComments# re: GridView DropDownList PagerRaj, Real good solution, i dont know how this simple skipped my View ! Thx pointing.. Also, dou have any pointers for implementing static headers in gridview ! As soon as we put the gridview
  3. 3. under div panel, [we have 20+ columns] any static header solutions screws up. Also the samecase for Atlas update panel ? Thx, GopiMonday, August 14, 2006 1:02 PM by Gopinath Varadharajan# GridView DropDownList PagerMonday, August 14, 2006 3:14 PM by -:[web caboodle]:-# GridView DropDownList PagerYouve been kicked (a good thing) - Trackback from DotNetKicks.comMonday, August 14, 2006 8:32 PM by DotNetKicks.com# re: GridView DropDownList PagerThis is the best solution anyone can think of. Great work. Thanks a lotSaturday, October 14, 2006 10:45 PM by Prashant Atal# re: GridView DropDownList Pageri got an error PresentationUtils doesnt exist in context. Any idea? Thanks a lot.Thursday, October 19, 2006 11:06 AM by amber001# re: GridView DropDownList PagerWhat is the PresentationUtils class? I cannot get my project to build with that in there. I love theconcept though!Tuesday, October 31, 2006 11:49 AM by Jon Whitehead# re: GridView DropDownList PagerPresentationUtils is a class you create which contains the method SetPagerStates.Wednesday, November 01, 2006 2:41 PM by rajbk# re: GridView DropDownList Pagerhello its nice and useful code. but as im not very good in delegates and events right now i have aproblem and i need to solve it quickly. thats it : what if i need to handle for example the sortingof a gridview in a separate class like u did i cant put += delegate{...} because i need theinformation provided by the "GridViewSortEventArgs" so my question is how to pass this
  4. 4. information because until i tried lots of things and the sorting is not handled until i use thedelegate{....} to simplify the thing what would you do if for your dropdownlist u need to use theargs of its eventhandler ? its very urgent i will be grateful for an answer sorry for my english thxin advanceThursday, November 09, 2006 12:23 PM by Mourad# re: GridView DropDownList Pagerunbelivably easy - thanks :o)Monday, December 04, 2006 5:56 PM by imbehemot# re: GridView DropDownList PagerGreat bit of code, cheers for sharing.Tuesday, December 12, 2006 9:12 AM by Chrismogz# re: GridView DropDownList PagerAny solution for no postbacks and use ajax callbacks with PagerTemplate?Sunday, December 17, 2006 4:35 AM by Gudus# re: GridView DropDownList Pagerthanks for your code. it help me complete my homework. I hope i can give you another codesFriday, December 22, 2006 3:30 AM by Hoai# re: GridView DropDownList PagerAwesome and yet simple code! it really helped me out with my YourCollegePapers.com site!Thanks a million!DaveFriday, January 19, 2007 9:52 AM by You are genius!# re: GridView DropDownList Pageruseful, simple codecheers!
  5. 5. Monday, January 29, 2007 12:27 AM by plibertine# re: GridView DropDownList PagerPls send this code in C#. PageIndex always gives 0.Thanks in advanceFriday, March 16, 2007 8:51 AM by Ashish# re: GridView DropDownList PagerAshish,The code *is* in C#.Friday, March 16, 2007 8:37 PM by rajbk# re: GridView DropDownList Pagertnx for this code!!exist a method to render pagertemplate if page number is 1??Wednesday, May 02, 2007 9:20 AM by ilbarzo# re: GridView DropDownList PagerThought people would enjoy seeing the .net vbscript version of this.Row Created Event Handler: Protected Sub SearchResultsGrid_RowCreated(ByVal sender As Object, ByVal e AsGridViewRowEventArgs) If e.Row.RowType = DataControlRowType.Pager Then utils.SetPagerButtonStates(SearchResultsGrid, e.Row) End If End SubReset the Index of your grid if the user uses the Drop-down (set to the OnSelectedIndexChangeproperty of the dropdown):
  6. 6. Protected Sub setIndex(ByVal sender As Object, ByVal e As EventArgs) SearchResultsGrid.PageIndex = sender.SelectedIndex End SubAnd finally I tweaked the code a bit for SetPagerButtonStates. I prefer to use Link buttons forstyle reasons, so Ive changed all the dim statements to LinkButton rather than Button.Public Shared Sub SetPagerButtonStates(ByVal gridView As GridView, ByVal gvPagerRow AsGridViewRow) Dim pageIndex As Integer = gridView.PageIndex Dim pageCount As Integer = gridView.PageCount Dim i As Integer Dim btnFirst As LinkButton = gvPagerRow.FindControl("btnFirst") Dim btnPrevious As LinkButton = gvPagerRow.FindControl("btnPrev") Dim btnNext As LinkButton = gvPagerRow.FindControl("btnNext") Dim btnLast As LinkButton = gvPagerRow.FindControl("btnLast") Dim lbltotal As Label = gvPagerRow.FindControl("pagetotal") If gridView.PageIndex <> 0 Then btnFirst.Enabled = True btnPrevious.Enabled = True Else btnFirst.Enabled = False btnPrevious.Enabled = False End If If gridView.PageIndex < (gridView.PageCount) - 1 Then btnLast.Enabled = True
  7. 7. btnNext.Enabled = True Else btnLast.Enabled = False btnNext.Enabled = False End If lbltotal.Text = gridView.PageCount Dim ddlPageSelector As DropDownList = gvPagerRow.FindControl("ddlPageSelector") ddlPageSelector.Items.Clear() For i = 1 To gridView.PageCount ddlPageSelector.Items.Add(i.ToString()) Next ddlPageSelector.SelectedIndex = pageIndex gridView.SelectedIndex = pageIndex End SubMonday, June 18, 2007 3:01 PM by skril# re: GridView DropDownList PagerThanks. A great solution.Monday, July 16, 2007 9:11 AM by Chris Williams# re: GridView DropDownList PagerExcellent and a very simplistic and to the point. Wish I had developers like you around. Not onlythe drop down list but touching upon the delegate stuff was excellent.Tuesday, July 17, 2007 5:55 PM by Anonymous# re: GridView DropDownList Pagerhi there...
  8. 8. how would i do if i want to have paging something like this:1 2 3 4 5 6 7 .. instead of dropdownlist?thanksSaturday, August 11, 2007 12:24 AM by Nisar Khan# Yurtman.net &raquo; Gridview de Dropdowlist kullanarak sayfalamaPingback from Yurtman.net &raquo; Gridview de Dropdowlist kullanarak sayfalamaFriday, August 17, 2007 12:15 PM by Yurtman.net » Gridview de Dropdowlist kullanaraksayfalama# re: GridView DropDownList PagerExcellent and Thanks for give us this kind of code .could you give me file scrabing code .Thursday, August 23, 2007 4:42 PM by Ankur# re: GridView DropDownList Pagerexcellent job...anyways thanx for a such nice help...Wednesday, August 29, 2007 4:12 AM by Mehar# re: GridView DropDownList PagerIn response to Nisars request for 1 2 3 4 etc., Ive implemented that as so:int pageIndex = gridView.PageIndex;int pageCount = gridView.PageCount - 1;int pageButtonCount = gridView.PagerSettings.PageButtonCount - 1;// We dont necessarily want page 0 to x, though. If the current page is > x / 2, then we want tocentre itint startingPage = 0;int endingPage = pageButtonCount;
  9. 9. int halfButtonCount = Convert.ToInt32(pageButtonCount / 2);if (pageIndex > halfButtonCount){startingPage = pageIndex - halfButtonCount;endingPage = pageIndex + halfButtonCount;Literal openingDots = new Literal();openingDots.Text = "... ";placeholderPageSelector.Controls.Add(openingDots);}if (endingPage > pageCount){// Obviously we dont want to link to more pages than there are given the size of the datasetendingPage = pageCount;}for (int i = startingPage; i <= endingPage; i++){if (i == pageIndex){Literal currentPageNumber = new Literal();currentPageNumber.Text = String.Format("{0}", i + 1);placeholderPageSelector.Controls.Add(currentPageNumber);}else
  10. 10. {LinkButton pageLink = new LinkButton();placeholderPageSelector.Controls.Add(pageLink);pageLink.Text = String.Format("{0}", i + 1);pageLink.CommandName = "Page";pageLink.CommandArgument = String.Format("{0}", i + 1);}Literal separator = new Literal();if (i < endingPage){separator.Text = ", ";}else if (i < pageCount){separator.Text = " ...";}placeholderPageSelector.Controls.Add(separator);}Hopefully thats clear enough.Im interested in anyone having any suggestions as to the easiest way to save this pager templateand accompanying code in, say, a User Control, so that you dont have to add it to every page.Or, for that matter, change it on every page if someone comes up with a better design.Cheers,Steve
  11. 11. Wednesday, August 29, 2007 10:36 AM by Steve O# re: GridView DropDownList PagerSteve,Thanks for your help.where do you add placeholderPageSelector?Friday, August 31, 2007 10:09 AM by Nisar Khan# re: GridView DropDownList Pagerdoes not work as expectedif i have a 5 rows its displaying me11111if i have 385 rows its displaying something like this:111, 2, 3, 4, 5, 6, 7, 8, 9, 10 ...1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ...1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ...1, 2, 3, 4, 5,6, 7, 8, 9, 10i have added<asp:PlaceHolder ID="placeholderPageSelector" runat="server"></asp:PlaceHolder>Friday, August 31, 2007 10:17 AM by Nisar Khan# re: GridView DropDownList PagerHi,This was a very helpful article for me.And it helped me a lot in my function.Wednesday, September 05, 2007 3:56 PM by Abhishek# re: GridView DropDownList PagerHi
  12. 12. I need to find out the current row of gridview while the items in the dropdownlist in that row getchanged.Now work fine until if the above rows dropdown empty.If already above row DDLhave value and I try to select the below row DDL the row values which is above selectedI used ASP.Net2.0 I include everthying,but if user came back aggain the back button problemhappenedThursday, September 27, 2007 6:14 AM by Natraj# Gridview de Dropdowlist kullanarak sayfalamaGridview de Dropdowlist kullanarak sayfalamaWednesday, October 24, 2007 3:20 PM by Yurtman.net# re: GridView DropDownList PagerInstead of raising an event on every row, you can just add an event OnPreRender. This way youonly catch the event once rather than once per row. The only change you need to make is infinding the controls just reference:GridView1.TopPagerRow.FindControl()Thursday, November 08, 2007 10:34 PM by Tom# re: GridView DropDownList PagerGreat code.Small problem. I turns out that with large number of pages ( I have 1300) the sequence belowintroduces a huge delay while navigating through pages. Apparently it takes more time to fill thecombo than it takes to get the data from the DB. (I suppose the rendering takes very long)for (int i = 1; i <= gridView.PageCount; i++) { ddlPageSelector.Items.Add(i); }RegardsTuesday, November 20, 2007 10:15 AM by mmmmmm# re: GridView DropDownList Pager
  13. 13. when i use the vb code i got a syntax error saying that Name Utils not decalred eventhough i hvimports system.web.utils...can guide meMonday, December 03, 2007 10:37 PM by dev Oneal# re: GridView DropDownList PagerHow would you add a total recrods count to this? My ulitimate goal is to display something likethis:Displaying records X of Y of Z total records.Ive been able to get the X and Y, but cant get the Z.. the total records in the GridView.Tried GridView.Rows.Count, but it doesnt work.Tuesday, December 25, 2007 12:11 AM by GuestUser# re: GridView DropDownList PagerhiCan i know how to declare utils for the following code:Protected Sub SearchResultsGrid_RowCreated(ByVal sender As Object, ByVal e AsGridViewRowEventArgs)If e.Row.RowType = DataControlRowType.Pager Thenutils.SetPagerButtonStates(SearchResultsGrid, e.Row)End If End SubThanxFriday, January 04, 2008 3:01 PM by Michelle# re: GridView DropDownList PagerWorks great and very simple to add in. Thanks!Sunday, March 02, 2008 10:43 PM by Daniel
  14. 14. # re: GridView DropDownList Pagermany thansk it was so helpfull for meSaturday, April 12, 2008 7:20 PM by ar# re: GridView DropDownList PagerVery Useful code thanks very muchFriday, May 16, 2008 6:24 AM by Sarawut# re: GridView DropDownList Pagerhi ..nice code am getting your code wen am using the same one if i change that as of myrequirement then wen am attempting to select a value from drop down list then thegridview1.pagecount becoming zero ..am trying a lot but am not getting plz help me...dharmaFriday, July 18, 2008 7:47 AM by dharma# re: GridView DropDownList Pagerexcellent done good workThursday, July 24, 2008 7:43 AM by saigopal# re: GridView DropDownList Pagerplease eplain in brief where pagertemplate should be placed .it taken a lot of time to found .by intotal ur program was very good;Thursday, July 24, 2008 7:45 AM by saigopal# re: GridView DropDownList Pagersuper its working fineTuesday, August 05, 2008 3:25 AM by Ramamoorthy# re: GridView DropDownList PagerThanks also for the vb.net code it really helped me a lotFriday, September 19, 2008 10:29 AM by mc
  15. 15. # re: GridView DropDownList PagerHi, i use this gridview dropdownlist pager in gridview1. then i use again in gridview2 in samepage. after i click next button in gridview2, it will not show gridview2 d. gridview2 disappear?how to solve?Wednesday, November 05, 2008 9:44 AM by k# re: GridView DropDownList Pagerthanks for code ;-)Tuesday, November 25, 2008 4:25 AM by mahnaz# re: GridView DropDownList PagerI must be stupid because I cant get the buttons to work in VB.net. The dropdown works like acharm though.Tuesday, December 09, 2008 6:17 AM by Mats# re: GridView DropDownList PagerI couldnt get my Next, Previous buttons to work.Later found out that I had:EnableViewState="false" defined. Take this away and they now work...Tuesday, December 23, 2008 7:16 AM by Richard Leiser# re: GridView DropDownList PagerI did get this to work in vb thanks to skril. I did have to make a couple of changes...FYI, for those having problems with the:utils.SetPagerButtonStates(SearchResultsGrid, e.Row)the "utils" is referring to a class so you would have it in a new Public Class utils area.I removed the:gridView.SelectedIndex = pageIndex
  16. 16. from the last line of the SetPagerButtonStates sub as this would obviously have the gridViewsselected index match that of the pageIndex which isnt nice.and this:Dim btnPrevious As LinkButton = gvPagerRow.FindControl("btnPrev")had to change to this:Dim btnPrevious As LinkButton = gvPagerRow.FindControl("btnPrevious")Here is what I am using this for:My grid view select linkbutton is updating 3 text labels for the gridview selectedvalue,pageindex, and selectedindex.The selectedvalue is updating a formview so while paging through the list my formview staysdisplayed on the selectedindex.The pageindex and selectedindex labels are used to compare when paging to see if I am on thesame page of the selectedvalue and if so to set the selectedindex to the value of my selectedindexlabel.If anyone is looking to do something similar, just email me and I will forward the code. In themean time, here is a link of where I am using it but will eventually be in a live environment areaand may not last for long here:test.rmpcaz.net/.../propsPaging.aspxSaturday, January 03, 2009 4:18 AM by JimmyS# re: GridView DropDownList Pagerhi guys ,i have to implement pre/next with numeric modelike 1 2 3 4 5 nextpre 6 7 8 9 10 nextplease tel me how to implemt this with complete discriptin ,as i am very new to custom pagingThursday, July 02, 2009 6:36 AM by mayank

×