[英]ASP.NET DataPager Control in a Server Control
我正在嘗試創建一個將使用DataPager控件的服務器控件,但在使用PagerTemplate時遇到了一些困難。
這是我想從服務器控件生成的DataPager控件:
<asp:DataPager ID="myPager" PageSize="20" runat="server">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<div class="counter">
<%# Container.StartRowIndex + 1 %> to
<%# ((Container.StartRowIndex + Container.PageSize) > Container.TotalRowCount ? Container.TotalRowCount : (Container.StartRowIndex + Container.PageSize)) %>
of <%# Container.TotalRowCount %> records
</div>
</PagerTemplate>
</asp:TemplatePagerField>
<asp:NextPreviousPagerField ButtonType="link"
FirstPageText="first"
ShowFirstPageButton="true"
ShowNextPageButton="false"
ShowPreviousPageButton="false"
RenderDisabledButtonsAsLabels="true" />
<asp:NumericPagerField ButtonCount="7" />
<asp:NextPreviousPagerField ButtonType="link"
LastPageText="last"
ShowLastPageButton="true"
ShowNextPageButton="false"
ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
我不知道如何從代碼創建PagerTemplate。 我陷入需要創建ITemplate的部分,但是我不知道如何使用它。
我已經做了一些搜索,但沒有找到任何可以幫助我的東西。 我是服務器控件的新手。 我可以做一些簡單的事情,但是模板對我來說是新的。
誰能在這方面給我一些幫助?
謝謝 :)
您需要創建一個實現ITemplate的類,以便以編程方式設置模板字段。 這是一個例子:
/// <summary>
/// A template that goes within a data pager template field to display record count information.
/// </summary>
internal class RecordTemplate : ITemplate
{
/// <summary>
/// Instantiates this template within a parent control.
/// </summary>
/// <param name="container"></param>
public void InstantiateIn(Control container)
{
DataPager pager = container.NamingContainer as DataPager;
if (pager != null)
{
pager.Controls.Add(new Literal()
{
Text = String.Format("Showing records {0} to {1} of {2}",
pager.StartRowIndex + 1,
Math.Min(pager.StartRowIndex + pager.PageSize, pager.TotalRowCount),
pager.TotalRowCount)
});
}
}
}
然后,在要創建DataPager的服務器控制代碼中,可以執行以下操作:
TemplatePagerField field = new TemplatePagerField();
field.PagerTemplate = new RecordTemplate();
MyDataPager.Fields.Add(field);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.