FormView不显示PagerTemplate

FormView不显示PagerTemplate

本文介绍了当数据源中只有一项时,FormView不显示PagerTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义PagerTemplate的FormView控件,该控件带有我自己的分页LinkBut​​ton.一切正常,直到我加载的数据集仅包含一个记录/项目并完全隐藏PagerTemplate.我在网上进行搜索,找到了几个答案,只需添加即可:

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

但是,这并没有什么不同.似乎在数据集中只有一条记录时,FormView.TopPagerRow始终为null.似乎需要一种方法来拦截它,但是我已经尝试在OnPreRender和OnDataBound事件中做到这一点,但是仍然没有骰子.

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>

隐藏代码

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

谢谢!

推荐答案

对此 http://forums.asp.net/t/1016811.aspx/1 ,对我来说效果很好.如果您想在首页中排首,我猜HeaderTemplate也可以.

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.

在我设置的FormView的标记中:

"In the markup for the FormView I set:

AllowPaging ="true"

AllowPaging="true"

PagerSettings-Visible ="false"

PagerSettings-Visible="false"

然后,将所有用于分页的控件放在FormView的FooterTemplate中.通过这种方式,即使在数据源中仅返回一条记录的情况下,我仍然能够处理所有分页事件,并始终使"Pager Row"可见."

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."

这篇关于当数据源中只有一项时,FormView不显示PagerTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:19