FormView not displaying PagerTemplate when only 1

2019-07-23 08:54发布

问题:

I have a FormView control with a custom PagerTemplate with my own paging LinkButtons. All works great until the dataset that I load only contains one record/item and hides the PagerTemplate completely. I've searched online and found several answers to simply add:

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

However, this is not making one bit of difference. It seems that when there is only one record in the data set, the FormView.TopPagerRow is always null. It seems that there needs to be a way to intercept this but I've tried doing it in both OnPreRender and OnDataBound events but still no dice.

Below is sample of my code if that helps:

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>

Codebehind

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;
}

thanks!

回答1:

Tried this http://forums.asp.net/t/1016811.aspx/1 and it worked fine for me. If you want a top pager row, I guess HeaderTemplate would also work.

"In the markup for the FormView I set:

AllowPaging="true"

PagerSettings-Visible="false"

Then I put all of my controls for paging in the FooterTemplate of the FormView. By doing it this way I was still able to handle all of the paging events and always have the "Pager Row" visible even when there was only one record being returned in the datasource."