我有一个带有自定义PagerTemplate的FormView控件,该控件带有我自己的分页LinkButtons。在我加载的数据集仅包含一个记录/项目并完全隐藏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;
}
谢谢!
最佳答案
尝试了这个http://forums.asp.net/t/1016811.aspx/1,对我来说效果很好。如果您想在首页中排首,我猜HeaderTemplate也可以。
“在我设置的FormView的标记中:
AllowPaging =“ true”
PagerSettings-Visible =“ false”
然后,将所有用于分页的控件放入FormView的FooterTemplate中。通过这种方式,即使在数据源中仅返回一条记录的情况下,我仍然能够处理所有分页事件,并且始终使“ Pager Row”可见。
关于c# - 当数据源中只有一项时,FormView不显示PagerTemplate,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13409173/