【问题标题】:ASP.NET DataPager Control in a Server Control服务器控件中的 ASP.NET DataPager 控件
【发布时间】:2010-11-05 18:39:10
【问题描述】:

我正在尝试创建一个将使用 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 的部分,但我不知道如何使用它。

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

谁能帮我解决这个问题?

谢谢:)

【问题讨论】:

    标签: asp.net controls datapager 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);
    

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 2011-03-02
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2015-11-21
      • 1970-01-01
      • 2011-02-15
      相关资源
      最近更新 更多