0

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を作成する必要がある部分で立ち往生していますが、それを操作する方法がわかりません。

いくつか検索しましたが、役立つものは見つかりませんでした。私はサーバーコントロールの初心者です。簡単なものもいくつかできますが、テンプレートは初めてです。

誰かが私にこれについていくつかの助けを与えることができますか?

ありがとう :)

4

1 に答える 1

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);
于 2011-01-11T22:31:14.567 に答える