处理中继器的c#等效句柄

时间:2014-07-11 14:26:03

标签: c# asp.net vb.net

我有以下ASP控件:

<div class="pageNumSec">
    <asp:LinkButton runat="server" ID="btnPrevious" OnClick="LinkButton1_Click" CssClass="directForBackBtn" Text="<< Previous Page"></asp:LinkButton>

    <asp:repeater runat="server" ID="rptPager">
        <ItemTemplate>
            <asp:LinkButton runat="server" ID="btnPageNumber" CssClass="pageNum" Text='<%#Container.DataItem%>' CommandArgument='<%#Container.DataItem%>'></asp:LinkButton>
        </ItemTemplate>
    </asp:repeater>

    <asp:LinkButton runat="server" ID="btnNext" OnClick="LinkButton1_Click"  CssClass="directForBackBtn" Text="Next Page >>"></asp:LinkButton>
</div>

这会将页码显示为链接。

这是代码,意图是:a)使这些链接更新页面&amp; b)以粗体突出显示当前页面:

//navigate by using the page number links
protected void rptPager_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
    _pageNumber = int.Parse(e.CommandArgument.ToString());
    PopulatePage(_pageNumber);
}

//make the current page bold
protected void rptPager_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    //is current item the current page.
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        int currentPage = Convert.ToInt32(e.Item.DataItem);
        LinkButton currentLinkBtn = (LinkButton)e.Item.FindControl("btnPageNumber");

        //if true, highlight text
        if (currentPage == _pageNumber || (currentPage == 1 && _pageNumber == 0))
        {
            currentLinkBtn.Font.Bold = true;
        }
    }
}

目前,当您与页面交互时,这些方法都没有触发,这些方法已经在VB.Net的C#中重写。我在原始代码中看到的唯一区别是VB包含Handles个关键字,如:

Protected Sub rptPager_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptPager.ItemCommand

    _pageNumber = Integer.Parse(e.CommandArgument)

    PopulatePage(_pageNumber)

End Sub



Protected Sub rptPager_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPager.ItemDataBound
    'is current item the current page.

    If e.Item.ItemType = ListItemType.AlternatingItem OrElse
        e.Item.ItemType = ListItemType.Item Then

        Dim currentPage As Integer = CType(e.Item.DataItem, Integer)
        Dim currentLinkBtn As LinkButton = CType(e.Item.FindControl("btnPageNumber"), LinkButton)

        'if true, highlight text
        If currentPage = _pageNumber OrElse (currentPage = 1 AndAlso _pageNumber = 0) Then
            currentLinkBtn.Font.Bold = True
        End If
    End If

End Sub

有人可以告诉我如何将这些方法连接到ASP,以便链接有效并且当前页码是粗体吗?

1 个答案:

答案 0 :(得分:3)

您需要将处理程序添加到Repeater:

<asp:Repeater runat="server" ID="rptPager"
    OnItemDataBound="rptPager_ItemDataBound"
    OnItemCommand="rptPager_ItemCommand">