2010-11-05 83 views
0

我正在尝试创建一个将使用DataPager控件的服务器控件,但我在使用PagerTemplate时遇到了一些困难。服务器控件中的ASP.NET DataPager控件

这是我想从一个服务器控件生成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的地方,但我不知道如何使用它。

我已经做了一些搜索,但没有找到任何可以帮助我的东西。我有点服务器控件的新手。我可以做一些简单的,但模板对我来说是新的。

任何人都可以给我一些帮助吗?

谢谢:)

回答

1

您需要创建一个实现了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);