自定义寻呼机控制

时间:2011-03-04 12:24:09

标签: asp.net paging repeater

我开发了一个自定义数据分页器控件。它的“上一个”和“下一个”超链​​接按钮无法正常工作,我不明白为什么。

    <asp:Repeater ID="rpt" runat="server">
<HeaderTemplate>
    <asp:LinkButton ID="lnkbFirst" CommandName="<%#PageChangedItemCommand %>" CommandArgument="1" runat="server">&laquo;</asp:LinkButton>&nbsp;
     <asp:LinkButton ID="lnkbPrevious" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PreviousPageIndex%>"  runat="server">&lt;</asp:LinkButton>&nbsp;
 </HeaderTemplate>   
<ItemTemplate> 
    <asp:LinkButton  CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#Container.DataItem.ToString()%>"  ID="p" runat="server" ><%#Container.DataItem.ToString()%>&nbsp;</asp:LinkButton>
</ItemTemplate>

<FooterTemplate>
     <asp:LinkButton ID="lnkbNext" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#NextPageIndex%>"  runat="server">&gt;</asp:LinkButton>&nbsp;
     <asp:LinkButton ID="lnkbLast" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PagesCount%>" runat="server">&raquo;</asp:LinkButton>
</FooterTemplate>
</asp:Repeater>



public partial class Pager : System.Web.UI.UserControl
    {
        public int CurrentPage { get; set; }
        public int PageSize { get; set; }
        public int DataItemsCount { get; set; }


        private int _pagesCount;
        public int PagesCount
        {

            get
            {
                if (DataItemsCount % PageSize == 0)
                    return _pagesCount = (DataItemsCount / PageSize);

                else
                    return _pagesCount = ((DataItemsCount / PageSize) + 1);
            }

            private set
            {
                _pagesCount = value;
            }
        }

        public event EventHandler<PageChangedEventArgs> PageChanged;
        protected const string PageChangedItemCommand = "PageChanged";
        private const string CurrentPageCssStyle = "font-weight:bold; font-size:15px;";

        private void RaiseEvent(int currentPage)
        {
            if (PageChanged != null)
                PageChanged(this, new PageChangedEventArgs(currentPage));
        }

        protected List<int> DataSource
        {
            get
            {
                List<int> pages = new List<int>();
                for (int i = 1; i <= PagesCount; i++)
                    pages.Add(i);

                return pages;
            }
        }

        protected int NextPageIndex { get { return CurrentPage + 1; } }
        protected int PreviousPageIndex { get { return CurrentPage -1; } }

        public Pager()
        {
            CurrentPage = 1;
            PageSize = 1;
            DataItemsCount = 1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
            rpt.DataSource = DataSource;
            rpt.DataBind();

          //  
            if (!Page.IsPostBack)
            {
                CurrentPageSetCssStyle(CurrentPageCssStyle);
                // SetupCommandArguments();
            }
        }

        void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == PageChangedItemCommand)
            {
                CurrentPage = int.Parse(e.CommandArgument.ToString());
                CurrentPageSetCssStyle(CurrentPageCssStyle);
               // SetupCommandArguments();
                RaiseEvent(CurrentPage);

            }
        }



        private void CurrentPageSetCssStyle(string style)
        {
            foreach (RepeaterItem item in rpt.Items)
            {
                LinkButton lnkButton = item.FindControl("p") as LinkButton;
                if (lnkButton != null)
                {
                    if (lnkButton.CommandArgument == CurrentPage.ToString())
                        lnkButton.Attributes.Add("style", style);
                }
            }
          //  SetupCommandArguments();
        }



        void SetupCommandArguments()
        {
            LinkButton lnkbPrevious = rpt.Controls[0].Controls[0].FindControl("lnkbPrevious") as LinkButton;
            if (lnkbPrevious != null)
                lnkbPrevious.CommandArgument = (CurrentPage - 1).ToString();

            LinkButton lnkbNext = rpt.Controls[rpt.Controls.Count - 1].Controls[0].FindControl("lnkbNext") as LinkButton;
            if (lnkbPrevious != null)
                lnkbPrevious.CommandArgument = (CurrentPage + 1).ToString();
        }

    }

我需要开发自己的分页灯控件。不建议我使用其他分页控件。

1 个答案:

答案 0 :(得分:1)

我在过去几天里不得不做一个自定义寻呼机,但我的情况略有不同。

我选择使用两个中继器: 这个是用于传呼的

<div class="searchResultsPaging" runat="server">
        <asp:Repeater ID="Paging" runat="server">
            <ItemTemplate>
                <a href='<%# Eval("PageUrl") %>' class='<%# (Convert.ToBoolean(Eval("IsCurrent")) == true)? "pagingButtonOff" : "pagingButtonOn" %>'>
                    <%# Eval("PageText") %></a>
            </ItemTemplate>
        </asp:Repeater>
    </div>

2。是我显示的项目

<asp:Repeater ID="Properties" runat="server">
        <ItemTemplate>
            <div class="propertyCard">
                <div class="propertyTitle">
                    <a href='/property/<%# Eval("Id") %>'>
                        <%# Eval("Title") %></a></div>
                <div class="propertySuburb">
                    <%# Eval("Suburb") %></div>
                <img src='/_Remove/SamplePropertyImages/<%# Eval("PriorityImage") %>' width="190"
                    height="143" alt='Property for sale in <%# Eval("Suburb")%>' />
                <div class="propertyFeatures">
                    <img src="/_Remove/Icons/Bed.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bedrooms") %> bedrooms.' /><%# Eval("Bedrooms") %><img
                        src="/_Remove/Icons/Bath.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bathrooms") %> bedrooms.' /><%# Eval("Bathrooms") %></div>
                <div class="propertyPrice">
                    <%# Eval("Price","{0:###,##0.00}") %>
                </div>
                <a href="#">View Property Details</a>
            </div>
        </ItemTemplate>
    </asp:Repeater>

在绑定期间,我的业务逻辑返回了属性列表和总计数。从那里很容易绑定和构建分页控件。

因为您正在寻找页面下一个/后一个类型的功能,所以可能不适合您。

此致 Jacqueds