?
Solved

Pager Web User Control

Posted on 2005-05-08
9
Medium Priority
?
314 Views
Last Modified: 2012-08-13
I am writing in VB.Net for an Asp.Net page.

Here is what i am trying to do.  

Create a custom pager Web User Control.  

I have these controls on the page:
Prev Link Button
Next Link Button
Last Link Button
First LinkButton
Page Number DropDownList
Number Per Page DropDownList

My Stored Procedure only returns the records needed; in the order they are requested; so there is no need to do that part on the Web User Control's code behind. That work is done in the aspx code behind.  I thought I could set properties in the Web User Control and that would just write out what was needed on the page such as the correct number of pages in the Page Number DropDownList and have the correct one selected or have the Last and Next LinkButtons disable if there were no more pages to go in that direction.

My thougth was that all i had to do was pass the Web User Control the:
Record Count
Number Per page
Page On

Then have it do the work needed to have all the controls work properly on the Web User Control page.

I have gotten nowhere on this.  Can anyone give me an example of how i can get this to work.  I would like to be able to place a pager at the bottom and the top if i could with one Web User Control.  The issue i see here is that the name of the controls on that Web User Control would be the same when the page loaded.


-Jason
0
Comment
Question by:JsonTerre1
  • 5
  • 4
9 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 13959351
I've actually done this before, though I implemented it as a custom control, and I would suggest you doing the same.  It would be easier to share between applications if is like this.

I believe you are on the right track with the required inputs.  What exactly do you need help with?, are you familiar with what you need to do to make a control like this?
0
 
LVL 1

Author Comment

by:JsonTerre1
ID: 13959494
raterus ,

I am not familiar with what i need to do.  This is my first time trying somthing like this.  

What i was hoping for was some sample code for the aspx page and the control page to see how i would implement this.

I was assuming i would make this a generic pager control so that i could use it on any type of record listing.

I would love it if you could help me out with this. Sounds like you did exaclty what i want to do.

-Jason
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 13959559
I'm just going to throw this at you, it's the code for the pager I did.  It is a custom control.  Basically it has the three properties you want (PageSize, RecordCount, CurrentPageIndex), and when they move the page, it raises it's own event to tell the aspx page to do something.  If you have questions let me know.

Imports System.Web.UI
Imports System.Web.UI.webcontrols
Imports System.ComponentModel

    Public Class Pager : Inherits Control
        Implements INamingContainer

        'This property specifies the size of the page appearing on the page, this is only used for calcuation purposes
        'and doesn't affect the actual size of the page in use
        <Bindable(True), Category("Appearance"), DefaultValue("10")> _
        Public Property PageSize() As Integer
            Get
                Return CInt(ViewState("_Pager_PageSize"))
            End Get
            Set(ByVal Value As Integer)
                ViewState("_Pager_PageSize") = Value
            End Set
        End Property

        'This property specifies the number of records in the resultset
        Public Property RecordCount() As Integer
            Get
                Return CInt(ViewState("_Pager_RecordCount"))
            End Get
            Set(ByVal Value As Integer)
                ViewState("_Pager_RecordCount") = Value
            End Set
        End Property

        'The current page that the control displays
        <Bindable(True), Category("Appearance"), DefaultValue("0")> _
        Public Property CurrentPageIndex() As Integer
            Get
                Return CInt(Viewstate("_Pager_CurrentPageIndex"))
            End Get
            Set(ByVal Value As Integer)
                ViewState("_Pager_CurrentPageIndex") = Value
                CreateChildControls()
            End Set
        End Property

        Public Event PageIndexChanged(ByVal sender As Object, ByVal e As PagerEventArgs)

        Private Function Spacer(ByVal numSpaces As Integer) As Literal
            Dim i As Integer, spaces As String
            For i = 0 To numSpaces - 1
                spaces = spaces & "&nbsp;"
            Next

            Dim litReturn As Literal = New Literal
            litReturn.Text = spaces
            Return litReturn
        End Function

        Private Sub LinkCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
            Dim pee As PagerEventArgs = New PagerEventArgs
            Dim ca As String = CStr(e.CommandArgument)

            If ca = "-1" Then
                pee.NewPageIndex = CurrentPageIndex - 1
            ElseIf ca = "+1" Then
                pee.NewPageIndex = CurrentPageIndex + 1
            Else
                pee.NewPageIndex = CInt(ca) - 1
            End If

            RaiseEvent PageIndexChanged(sender, pee)
        End Sub

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()

            Dim pnlPageLinks As PlaceHolder = New PlaceHolder

            Dim StartResult As Integer, CurrentPage As Integer = CurrentPageIndex + 1
            StartResult = PageSize * CurrentPage '=130

            If CurrentPage > 1 Then
                'show "Previous"
                Dim hypPrevious As LinkButton = New LinkButton
                hypPrevious.Text = "Previous"
                hypPrevious.CommandArgument = "-1"
                hypPrevious.ID = "hypPrevious"
                hypPrevious.CssClass = "b"
                hypPrevious.CausesValidation = False
                AddHandler hypPrevious.Command, AddressOf LinkCommand
                pnlPageLinks.Controls.Add(hypPrevious)
                pnlPageLinks.Controls.Add(Spacer(3))

                Dim BackPages As Integer = StartResult - (PageSize * 10)
                While BackPages < StartResult
                    'add a backpage link
                    If BackPages / PageSize > 0 Then
                        Dim hypPage As LinkButton = New LinkButton
                        hypPage.Text = CStr(BackPages / PageSize)
                        hypPage.CommandArgument = hypPage.Text
                        hypPage.ID = "hyp" & hypPage.text
                        hypPage.CausesValidation = False
                        pnlPageLinks.Controls.Add(hypPage)
                        AddHandler hypPage.Command, AddressOf LinkCommand
                        pnlPageLinks.Controls.Add(Spacer(2))
                    End If
                    BackPages = BackPages + PageSize
                End While

            End If

            'write current page
            Dim lblCurrent As Label = New Label
            lblCurrent.Text = CurrentPage.ToString
            lblCurrent.ForeColor = System.Drawing.Color.FromName("#a90a08")
            pnlPageLinks.Controls.Add(lblCurrent)
            pnlPageLinks.Controls.Add(Spacer(2))
            StartResult = StartResult + PageSize

            Dim ForwardPages As Integer = StartResult + (PageSize * 9)
            While StartResult < ForwardPages AndAlso StartResult < RecordCount + PageSize
                'add a forwardpage link
                Dim hypPage As LinkButton = New LinkButton
                hypPage.Text = CStr(StartResult / PageSize)
                hypPage.CommandArgument = hypPage.Text
                hypPage.ID = "hyp" & hypPage.text
                hypPage.CausesValidation = False
                AddHandler hypPage.Command, AddressOf LinkCommand
                pnlPageLinks.Controls.Add(hypPage)
                pnlPageLinks.Controls.Add(Spacer(2))
                StartResult = StartResult + PageSize
            End While

            StartResult = StartResult - PageSize

            If CurrentPage < StartResult / PageSize Then
                'show next
                Dim hypNext As LinkButton = New LinkButton
                hypNext.Text = "Next"
                hypNext.CommandArgument = "+1"
                hypNext.ID = "hypNext"
                hypNext.CssClass = "b"
                hypNext.CausesValidation = False
                AddHandler hypNext.Command, AddressOf LinkCommand
                pnlPageLinks.Controls.Add(hypNext)
                pnlPageLinks.Controls.Add(Spacer(3))
            End If

            Controls.Add(pnlPageLinks)

        End Sub

    End Class

    Public Class PagerEventArgs
        Public NewPageIndex As Integer
    End Class
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:JsonTerre1
ID: 13964713
raterus ,

Wow.  Thanks for the code.  I am not sure how I am to use this on the aspx code behind page nor do i know how to reference this in the html.  

This is beyond my level but you just gave me a huge push into a new area of learning.  This is great stuff.

Thanks
-Jason
0
 
LVL 33

Expert Comment

by:raterus
ID: 13964902
yeah, it took me awhile to write that, if I hadn't written it so long ago, I probably wouldn't have given it out so quickly! :^)  This article should touch on most of the concepts I used here, and how to insert it into your aspx page.  For your reference this is called a "composite control".

http://samples.gotdotnet.com/quickstart/aspplus/doc/webctrlauthoring.aspx
0
 
LVL 1

Author Comment

by:JsonTerre1
ID: 13964969
I figured out how to insert it into the page and even changed the numbers to a DropDownList.

But the way I am testing is by putting the values in the tag.  I still havn't figured out how to set them in the code behind.  It seems like the control renders before i set the properties.

Thanks a bunch.  If you want I will post my changes back here for you to use if you want to.

-Jason
0
 
LVL 33

Expert Comment

by:raterus
ID: 13965091
setting properties shouldn't be any different than any other control you are using with asp.net.
0
 
LVL 1

Author Comment

by:JsonTerre1
ID: 13965120
raterus,

How would I capture a dropdownlist event in a custom control? I Have the DropDownList that the user selects to change the page number and i have it set to call a specific function but i can't figure out how to capture the SelectedValue.  

        Private Sub ddlPageNumberCommand( _
                        ByVal sender As Object, _
                        ByVal e As System.EventArgs)

     End Sub

-Jason
0
 
LVL 33

Expert Comment

by:raterus
ID: 13965125
If that is the sub that is called on SelectedIndexChanged of the ddl, and you are using my code (for the most part) it would be something like this

Private Sub ddlPageNumberCommand( _
                        ByVal sender As Object, _
                        ByVal e As System.EventArgs)

            Dim pee As PagerEventArgs = New PagerEventArgs
            pee.NewPageIndex = cint(ddlPageNumber.SelectedValue) - 1 'assuming your values=page numbers
            RaiseEvent PageIndexChanged(sender, pee)
     End Sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question