尝试将处理程序附加到linkbutton

时间:2018-05-04 21:45:25

标签: asp.net vb.net

你好我想让这个传呼机工作。我在将事件处理程序添加到pagerCommand事件时遇到了一些麻烦。我究竟做错了什么?我可以将处理程序附加到下拉列表没有问题,但链接有问题。任何指导将不胜感激。感谢

   Public Class DataPagerDDL
    Inherits DataPager

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        Me.CreateDefaultPagerFields()
        MyBase.OnInit(e)
    End Sub

    Protected Sub CreateDefaultPagerFields()

        'add custom template
        Dim templateField As TemplatePagerField = New TemplatePagerField
        templateField.PagerTemplate = New CustomTemplate
        'add previous/next page template
        Fields.Add(templateField)
    End Sub

    Public Sub cmbPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim cmbPage As DropDownList = CType(sender, DropDownList)
        SetPageProperties((cmbPage.SelectedIndex * MaximumRows), MaximumRows, True)
    End Sub
    Public Sub cmb_PagerCommand(ByVal sender As Object, ByVal e As DataPagerCommandEventArgs)
        ' Check which button raised the event
        Select Case e.CommandName

            Case "Next"
                Dim newIndex As Integer = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize
                If newIndex <= e.TotalRowCount Then
                    e.NewStartRowIndex = newIndex
                    e.NewMaximumRows = e.Item.Pager.MaximumRows
                End If

            Case "Previous"
                e.NewStartRowIndex = e.Item.Pager.StartRowIndex - e.Item.Pager.PageSize
                e.NewMaximumRows = e.Item.Pager.MaximumRows

            Case "First"
                e.NewStartRowIndex = 0
                e.NewMaximumRows = e.Item.Pager.MaximumRows
            Case "Last"
                Dim newIndex As Integer = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize
                If newIndex <= e.TotalRowCount Then
                    e.NewStartRowIndex = newIndex
                    e.NewMaximumRows = e.Item.Pager.MaximumRows
                End If
        End Select
    End Sub
End Class
Public Class CustomTemplate
    Implements System.Web.UI.ITemplate
    Dim PageCount As Integer
    Dim CurrentPage As Integer
    Dim PageSize As Integer
    Dim TotalRowCount As Integer
    Dim MaximumRows As Integer
    Dim StartRowIndex As Integer

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

        Dim caller As DataPagerFieldItem = CType(container, DataPagerFieldItem)
        Dim pager As DataPagerDDL = CType(caller.Parent, DataPagerDDL)
        PageSize = pager.PageSize
        TotalRowCount = pager.TotalRowCount
        MaximumRows = pager.MaximumRows
        PageCount = (TotalRowCount / MaximumRows)
        If ((pager.TotalRowCount Mod pager.MaximumRows) > 0) Then
            PageCount = (PageCount + 1)
        End If

        Dim link As LinkButton = New LinkButton
        'first Link
        link.Text = "<< First "
        link.CommandName = "Page"
        link.CommandArgument = "First"
        link.ID = "lnkFirst"
        link.Enabled = StartRowIndex > 0
        container.Controls.Add(link)

        'prev link
        link = New LinkButton
        link.Text = "< Prev "
        link.CommandName = "Page"
        link.CommandArgument = "Previous"
        link.ID = "lnkPrev"
        link.Enabled = StartRowIndex > 0
        container.Controls.Add(link)

        CurrentPage = ((StartRowIndex / MaximumRows) + 1)
        Dim cmbPage As DropDownList = New DropDownList
        cmbPage.ID = "cmbPage"
        cmbPage.AutoPostBack = True
        Dim i As Integer = 1
        Do While (i <= PageCount)
            Dim item As ListItem = New ListItem(i.ToString, i.ToString)
            If (i = CurrentPage) Then
                item.Selected = True
            End If

            cmbPage.Items.Add(item)
            i = (i + 1)
        Loop
        AddHandler cmbPage.SelectedIndexChanged, AddressOf pager.cmbPage_SelectedIndexChanged
        'add nav buttons

        ' we just add a Label with 'Page ' Text
        container.Controls.Add(PageOf())

        ' our DropDownList control here.
        container.Controls.Add(cmbPage)


        ' and our Total number of Pages
        container.Controls.Add(PageTotal())

        'next link
        link = New LinkButton
        link.Text = "Next > "
        link.CommandName = "Page"
        link.CommandArgument = "Next"
        link.ID = "lnkNext"
        link.Enabled = StartRowIndex + PageSize < TotalRowCount
        container.Controls.Add(link)

        'last link
        link = New LinkButton
        link.Text = "Last >> "
        link.CommandName = "Page"
        link.CommandArgument = "Last"
        link.ID = "lnkLast"
        link.Enabled = StartRowIndex + PageSize < TotalRowCount

        'Problem line
         AddHandler pager.cmb_PagerCommand, New EventHandler(AddressOf pager.cmb_PagerCommand)

        container.Controls.Add(link)

        container.Controls.Add(PageInfo(TotalRowCount))

    End Sub
    Private Function PageOf() As Label
        ' it is just a label
        Dim lbl As New Label()
        lbl.Text = "&nbsp;Page "
        Return lbl
    End Function

    Private Function PageTotal() As Label
        ' a label of GridView's Page Count
        Dim lbl As New Label()
        lbl.Text = String.Format(" of {0} ", PageCount)
        Return lbl
    End Function

    Private Function PageInfo(ByVal rowCount As Integer) As Label
        ' create a label that will display the current Record you're in
        Dim label As New Label()
        Dim currentPageFirstRow As Integer = ((CurrentPage * PageSize) + 1)
        Dim currentPageLastRow As Integer = 0
        Dim lastPageRemainder As Integer = rowCount Mod PageSize
        currentPageLastRow = IIf((PageCount = CurrentPage + 1), (currentPageFirstRow + lastPageRemainder - 1), (currentPageFirstRow + PageSize - 1))
        label.Text = [String].Format("Record {0} to {1} of {2}", currentPageFirstRow, currentPageLastRow, rowCount)
        Return label
    End Function

End Class

DataPager.png 这就是我对gridview的看法,我试图在一个独立的寻呼机中复制这个功能。此刻有点难过

1 个答案:

答案 0 :(得分:-1)

尝试替换以下代码行

 AddHandler pager.cmb_PagerCommand, New EventHandler(AddressOf pager.cmb_PagerCommand)

AddHandler link.Command, AddressOf pager.cmb_PagerCommand