
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:
DropDownList paging control
- Arrows paging if you prefer to use them over the
DropDownList
- Total records count
- Print button
- 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.
|
|
 |
 | Modification to Code Joshua Blackstone | 10: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 = " " 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 = " " cell.Controls.Add(spaceLabel5)
Dim pageLabel As New Label pageLabel.ID = "CurrentPageLabel" cell.Controls.Add(pageLabel)
Dim spaceLabel4 As New Label spaceLabel4.Text = " " 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 = " " 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 = " " cell.Controls.Add(spaceLabel7)
Dim ltrlSpan As New Label() ltrlSpan.Text = pagedDataSource.DataSourceCount.ToString() & " record(s) 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
|
|
|
|
 |
|
|
Last Updated 6 May 2009 |
Advertise |
Privacy |
Terms of Use |
Copyright ©
CodeProject, 1999-2010