Click here to Skip to main content
Licence CPOL
First Posted 6 May 2009
Views 17,252
Downloads 415
Bookmarked 19 times

FormView with a Paging Template

By Joshua Blackstone | 6 May 2009
What it would take to create a pager template that is similar to a good GridView paging template.

1

2
1 vote, 25.0%
3

4
3 votes, 75.0%
5
4.50/5 - 4 votes
μ 4.50, σa 2.02 [?]

FormViewModified

Introduction

I've seen several good GridView pager templates around, but very few FormView templates. I wanted to see what it would take to create a pager template that was similar to a good GridView paging template, and I think I've come up with one. I was surprised at how similar it actually was to creating a GridView paging template, but there are some distinct differences.

Background

The control I've created is called FormViewModified, and its paging template have the following characteristics:

  1. DropDownList paging control
  2. Arrows paging if you prefer to use them over the DropDownList
  3. Total records count
  4. Print button
  5. Word export button

Using the Code

To create a pager template, you first must set the AllowPaging attribute to True. Next, for a server control, you must override the InitializePager method with the following statement:

Protected Overrides Sub InitializePager( _ 
   ByVal row As FormViewRow, _ 
   ByVal pagedDataSource As PagedDataSource )

If you've created a server control using the AllowPaging paging templates, you'll notice it looks similar to the Overrides statement used for creating a GridView pager template:

Protected Overrides Sub InitializePager( _
   ByVal row As GridViewRow, _
   ByVal columnSpan As Integer, _
   ByVal pagedDataSource As PagedDataSource)

In either instance, FormView or GridView, this is where you will create your interface for the paging template. For example, if you want to create a label, you would do that by using the following logic:

Protected Overrides Sub InitializePager( _ 
   ByVal row As FormViewRow, _ 
   ByVal pagedDataSource As PagedDataSource) 

    Dim cell As New TableCell() 
    Dim Label As New Label 
    Label.ID = "Label1" 
    MessageLabel.Text = "I am a Label: " 
    cell.Controls.Add(Label) 
End Sub 

Next, you need to add your control functionality in the Formview_Databound event. A quick example would be if you wanted to add the page number to the Label that was created in the previous page. This would look similar to the following:

Private Sub FormViewModified_DataBound(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.DataBound 

    Dim pageLabel As Label = _
        CType(pagerRow.FindControl("Label"), Label) 

    If pageLabel IsNot Nothing Then 
    Dim currentPage As Integer = Me.PageIndex + 1 
    pageLabel.Text = "Page " & currentPage.ToString() & _
                     " of " & Me.PageCount.ToString() 
    End If 

End Sub 

Once this is complete with any controls you wish to add to handle the paging logic, the the pager is complete.

I have an example available for download, feel free to modify it to suit your needs. It adds a pager template to the FormView with the template displayed at the top of the FormView. It contains a few controls under the paging logic which I have found come in handy in almost all FormViews.

The full source code for this as well as other server controls that I've created, along with a sample website, is available for download from: http://www.joshuablackstone.com/Articles/JGBSolution.zip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Joshua Blackstone

Other
SRI Incorporated
United States United States

Member
I am an Application Developer in Indianapolis, IN. I work primarily with ASP.NET (VB), and Microsoft Access.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey20:43 3 Feb '12  
GeneralModification to Code PinmemberJoshua Blackstone10:11 18 Jun '09  
Wanted to modify to embed the images, cannot modify this article for some reason.
 
If you're interested, you can download the updated version by going to:
 
http://www.joshuablackstone.com/JGBSolution.zip .
Here, I simply changed the InitializePager method to read:
 
Protected Overrides Sub InitializePager( _
ByVal row As FormViewRow, _
ByVal pagedDataSource As PagedDataSource _
)
 
Dim cell As New TableCell()
cell.Controls.Add(New LiteralControl("<div class='none'>"))
Dim MessageLabel As New Label
MessageLabel.ID = "MessageLabel"
MessageLabel.Text = "Select a Page: "
cell.Controls.Add(MessageLabel)
 
Dim pageList As New DropDownList
pageList.ID = "PageDropDownList"
pageList.AutoPostBack = True
AddHandler pageList.SelectedIndexChanged, AddressOf PageDropDownList_SelectedIndexChanged
cell.Controls.Add(pageList)
 
Dim buttonFirst As New ImageButton
buttonFirst.ID = "ImageButtonFirst"
buttonFirst.CommandName = "Page"
buttonFirst.CommandArgument = "First"
buttonFirst.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "JGB.PgFirst.gif")
cell.Controls.Add(buttonFirst)
 
Dim spaceLabel3 As New Label
spaceLabel3.Text = "&nbsp;&nbsp;"
cell.Controls.Add(spaceLabel3)
 
Dim buttonPrevious As New ImageButton
buttonPrevious.ID = "ImageButtonPrevious"
buttonPrevious.CommandName = "Page"
buttonPrevious.CommandArgument = "Prev"
buttonFirst.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "JGB.PgPrev.gif")
cell.Controls.Add(buttonPrevious)
 
Dim spaceLabel5 As New Label
spaceLabel5.Text = "&nbsp;&nbsp;"
cell.Controls.Add(spaceLabel5)
 
Dim pageLabel As New Label
pageLabel.ID = "CurrentPageLabel"
cell.Controls.Add(pageLabel)
 
Dim spaceLabel4 As New Label
spaceLabel4.Text = "&nbsp;&nbsp;"
cell.Controls.Add(spaceLabel4)
 
Dim buttonNext As New ImageButton
buttonNext.ID = "ImageButtonNext"
buttonNext.CommandName = "Page"
buttonNext.CommandArgument = "Next"
buttonFirst.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "JGB.PgNext.gif")
cell.Controls.Add(buttonNext)
 
Dim spaceLabel6 As New Label
spaceLabel6.Text = "&nbsp;&nbsp;"
cell.Controls.Add(spaceLabel6)
 
Dim buttonLast As New ImageButton
buttonLast.ID = "ImageButtonLast"
buttonLast.CommandName = "Page"
buttonLast.CommandArgument = "Last"
buttonFirst.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "JGB.PgLast.gif")
cell.Controls.Add(buttonLast)
 
Dim spaceLabel7 As New Label
spaceLabel7.Text = "&nbsp;&nbsp;"
cell.Controls.Add(spaceLabel7)
 
Dim ltrlSpan As New Label()
ltrlSpan.Text = pagedDataSource.DataSourceCount.ToString() & "&nbsp;record(s)&nbsp;found"
cell.Controls.Add(ltrlSpan)
 
Dim spaceLabel8 As New Label
spaceLabel8.Text = "<br />"
cell.Controls.Add(spaceLabel8)
 
Dim PrintButton As New LinkButton
PrintButton.Text = "Print"
PrintButton.ID = "PrintButton"
PrintButton.Attributes.Add("onclick", "javascript:window.print();")
cell.Controls.Add(PrintButton)
 
cell.Controls.Add(New LiteralControl(" | "))
 
Dim CloseButton As New LinkButton
CloseButton.Text = "Close"
CloseButton.ID = "CloseButton"
CloseButton.Attributes.Add("onclick", "window.close();")
cell.Controls.Add(CloseButton)
 
cell.Controls.Add(New LiteralControl("</div>"))
row.Cells.Add(cell)
End Sub
 

Also includes other server controls that you may or may not wish to use.
Josh

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 6 May 2009
Article Copyright 2009 by Joshua Blackstone
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid