服务器控件中的ASP.NET DataPager控件 [英] ASP.NET DataPager Control in a Server Control

查看:84
本文介绍了服务器控件中的ASP.NET DataPager控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a Server Control that will use a DataPager Control, but I'm having some difficulties with the PagerTemplate.

这是DataPager控件我想从服务器控件生成的内容:

This is the DataPager control that I want to generate from a Server Control:

    <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的部分,但是我不知道如何使用它。

I don't know how to create the PagerTemplate from code. I'm stuck in a part where I need to create a ITemplate, but I don't know how to work with it.

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

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?

谢谢:)

推荐答案

您需要创建一个实现ITemplate的类,以便以编程方式设置模板字段。这是一个示例:

You need to create a class that implements ITemplate in order to set a template field programmatically. 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)
                });
            }
        }
    }

然后在服务器控制代码中在创建DataPager的位置,可以执行以下操作:

Then in your server control code where you are creating the DataPager you can do the following:

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

这篇关于服务器控件中的ASP.NET DataPager控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆