简体   繁体   English

服务器控件中的ASP.NET DataPager控件

[英]ASP.NET DataPager Control in a Server Control

I'm trying to create a Server Control that will use a DataPager Control, but I'm having some difficulties with the PagerTemplate. 我正在尝试创建一个将使用DataPager控件的服务器控件,但在使用PagerTemplate时遇到了一些困难。

This is the DataPager control that I want to generate from a Server Control: 这是我想从服务器控件生成的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>

I don't know how to create the PagerTemplate from code. 我不知道如何从代码创建PagerTemplate。 I'm stuck in a part where I need to create a ITemplate, but I don't know how to work with it. 我陷入需要创建ITemplate的部分,但是我不知道如何使用它。

I've done some search but haven't found anything that could help me. 我已经做了一些搜索,但没有找到任何可以帮助我的东西。 I'm a bit newbie with Server Controls. 我是服务器控件的新手。 I can do some simple ones, but templates are new to me. 我可以做一些简单的事情,但是模板对我来说是新的。

Can anyone give me some help on this? 谁能在这方面给我一些帮助?

Thanks :) 谢谢 :)

You need to create a class that implements ITemplate in order to set a template field programmatically. 您需要创建一个实现ITemplate的类,以便以编程方式设置模板字段。 Here is an example: 这是一个例子:

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

Then in your server control code where you are creating the DataPager you can do the following: 然后,在要创建DataPager的服务器控制代码中,可以执行以下操作:

TemplatePagerField field = new TemplatePagerField();
field.PagerTemplate = new RecordTemplate();
MyDataPager.Fields.Add(field);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM