当DataSource中只有1个项目时,FormView不显示PagerTemplate

时间:2012-11-16 01:40:38

标签: c# asp.net datasource formview pagertemplate

我有一个带有自定义PagerTemplate的FormView控件和我自己的分页LinkBut​​tons。一切都很好,直到我加载的数据集只包含一个记录/项目并完全隐藏PagerTemplate。我在网上搜索过,发现了几个答案,只需添加:

protected void fvAppHMDA_PreRender(object sender, EventArgs e)
    {
        if (fvAppHMDA.TopPagerRow != null)
            fvAppHMDA.TopPagerRow.Visible = true;
    }

但是,这并没有带来任何不同。看来,当数据集中只有一条记录时,FormView.TopPagerRow始终为null。似乎需要有一种方法来拦截这个,但我已经尝试在OnPreRender和OnDataBound事件中进行,但仍然没有骰子。

如果有帮助,下面是我的代码示例:

ASPX

<asp:FormView ID="fvAppHMDA" runat="server" AllowPaging="True" DefaultMode="Edit" DataSourceID="dsLoanApplication" Width="100%" onpageindexchanging="fvAppHMDA_PageIndexChanging" OnDataBound="fvAppHMDA_DataBound" OnPageIndexChanged="fvAppHMDA_PageIndexChanged" OnItemCreated="fvAppHMDA_ItemCreated" DataKeyNames="ApplicationID" OnPreRender="fvAppHMDA_PreRender">
<PagerTemplate>
    <div class="tab_toolbar">
        <asp:LinkButton ID="lnkMoveFirst" runat="server" 
            ToolTip="Move to the First Application" CommandName="Page" 
            CommandArgument="First"><img src="images/icons/Icon_ArrowBackEnd.png" width="16" height="16" class="grid2" alt="Move to the First Application" /></asp:LinkButton>
        <asp:LinkButton ID="lnkMovePrevPage" runat="server" 
            ToolTip="Move to the Previous Page" CommandName="Page" 
            onclick="lnkMovePrevPage_Click" Enabled="False"><img src="images/icons/Icon_ArrowBackSkip.png" width="16" height="16" class="grid2" alt="Move to the Previous Page" /></asp:LinkButton>
        <asp:LinkButton ID="lnkMovePrevApp" runat="server" 
            ToolTip="Move to the Previous Application" CommandName="Page" 
            CommandArgument="Prev"><img src="images/icons/Icon_ArrowBack.png" width="16" height="16" class="grid2" alt="Move to the Previous Application" /></asp:LinkButton>
        <div class="grid2_container"><asp:TextBox ID="txtMoveNumber" runat="server" Text="0" Columns="5"></asp:TextBox> of <asp:Label ID="lblMoveTotal" runat="server" Text="0"></asp:Label></div>
        <asp:LinkButton ID="lnkMoveNextApp" runat="server" 
            ToolTip="Move to the Next Application" CommandName="Page" 
            CommandArgument="Next"><img src="images/icons/Icon_ArrowForward.png" width="16" height="16" class="grid2" alt="Move to the Next Application" /></asp:LinkButton>
        <asp:LinkButton ID="lnkMoveNextPage" runat="server" 
            ToolTip="Move to the Next Page" CommandName="Page" 
            onclick="lnkMoveNextPage_Click" Enabled="False"><img src="images/icons/Icon_ArrowForwardSkip.png" width="16" height="16" class="grid2" alt="Move to the Next Page" /></asp:LinkButton>
        <asp:LinkButton ID="lnkMoveLast" runat="server" 
            ToolTip="Move to the Last Application" CommandName="Page" 
            CommandArgument="Last"><img src="images/icons/Icon_ArrowForwardEnd.png" width="16" height="16" class="grid2" alt="Move to the Last Application" /></asp:LinkButton>
    </div>
</PagerTemplate>
<PagerSettings Mode="NextPreviousFirstLast" Position="Top" />
<EditItemTemplate>
    <!-- FORM CONTROLS HERE -->
</EditItemTemplate>

代码隐藏

protected void fvAppHMDA_PreRender(object sender, EventArgs e)
{
if (fvAppHMDA.TopPagerRow != null)
    fvAppHMDA.TopPagerRow.Visible = true;
}

protected void fvAppHMDA_DataBound(object sender, EventArgs e)
{
if (fvAppHMDA.DataItemCount != 0)
{
    // Update Current and Total Page
    ((Label)fvAppHMDA.TopPagerRow.FindControl("lblMoveTotal")).Text = fvAppHMDA.PageCount.ToString();
    ((TextBox)fvAppHMDA.TopPagerRow.FindControl("txtMoveNumber")).Text = (fvAppHMDA.PageIndex + 1).ToString();

    // Enable/Disable Page buttons based on Current Page
    if (fvAppHMDA.PageIndex + 1 == fvAppHMDA.PageCount)
    {
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMoveLast")).Enabled = false;
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMoveNextApp")).Enabled = false;
    }
    else if (fvAppHMDA.PageIndex == 0)
    {
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMoveFirst")).Enabled = false;
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMovePrevApp")).Enabled = false;
    }

    if (fvAppHMDA.PageIndex + 10 <= fvAppHMDA.PageCount - 1)
    {
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMoveNextPage")).Enabled = true;
    }
    if (fvAppHMDA.PageIndex - 10 > -1)
    {
        ((LinkButton)fvAppHMDA.TopPagerRow.FindControl("lnkMovePrevPage")).Enabled = true;
    }

    fvAppHMDA.TopPagerRow.Cells[0].Visible = true;
}

if (fvAppHMDA.TopPagerRow != null)
    fvAppHMDA.TopPagerRow.Visible = true;
}

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试了这个http://forums.asp.net/t/1016811.aspx/1,它对我来说很好。如果你想要一个顶级寻呼机行,我猜HeaderTemplate也可以工作。

“在我设置的FormView的标记中:

AllowPaging = “真”

PagerSettings-可见= “假”

然后我将所有控件的调页放在FormView的FooterTemplate中。通过这种方式,我仍然能够处理所有的分页事件,并且即使在数据源中只返回了一条记录,也始终可以看到“Pager Row”。“