CustomList.cs :  » Template-Engines » netTiers » PetShop » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Template Engines » netTiers 
netTiers » PetShop » Controls » CustomList.cs
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PetShop.Controls{
    public class CustomList : DataList
    {
        //Static constants
        protected const string HTML1 = "<table cellpadding=0 cellspacing=0><tr><td colspan=2>";
        protected const string HTML2 = "</td></tr><tr><td class=paging align=left>";
        protected const string HTML3 = "</td><td align=right class=paging>";
        protected const string HTML4 = "</td></tr></table>";
        private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
        private const string LINK_PREV = "<a href=?page={0}>&#060;&nbsp;Previous</a>";
        private const string LINK_MORE = "<a href=?page={0}>More&nbsp;&#062;</a>";
        private const string KEY_PAGE = "page";
        private const string COMMA = "?";
        private const string AMP = "&";

        protected string emptyText;
        private IList dataSource;
        private int pageSize = 10;
        private int currentPageIndex;

        public override object DataSource
        {
            set
            {
                //This try catch block is to avoid issues with the VS.NET designer
                //The designer will try and bind a datasource which does not derive from ILIST
                try
                {
                    dataSource = (IList) value;
                    ItemCount = dataSource.Count;
                }
                catch
                {
                    dataSource = null;
                    ItemCount = 0;
                }
            }
        }

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        protected int PageCount
        {
            get { return (ItemCount - 1)/pageSize; }
        }

        protected virtual int ItemCount { get; set; }

        public virtual int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        public string EmptyText
        {
            set { emptyText = value; }
        }

        public void SetPage(int index)
        {
            OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
        }

        protected override void OnLoad(EventArgs e)
        {
            if (Visible)
            {
                string page = Context.Request[KEY_PAGE];
                int index = (page != null) ? int.Parse(page) : 0;
                SetPage(index);
            }
        }


        /// <summary>
        /// Overriden method to control how the page is rendered
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            //Check there is some data attached
            if (ItemCount == 0)
            {
                writer.Write(emptyText);
                return;
            }

            //Mask the query
            string query = Context.Request.Url.Query.Replace(COMMA, AMP);
            query = RX.Replace(query, string.Empty);


            // Write out the first part of the control, the table header
            writer.Write(HTML1);

            // Call the inherited method
            base.Render(writer);

            // Write out a table row closure
            writer.Write(HTML2);

            //Determin whether next and previous buttons are required
            //Previous button?
            if (currentPageIndex > 0)
                writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));

            //Close the table data tag
            writer.Write(HTML3);

            //Next button?
            if (currentPageIndex < PageCount)
                writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query));

            //Close the table
            writer.Write(HTML4);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            //Work out which items we want to render to the page
            int start = CurrentPageIndex*pageSize;
            int size = Math.Min(pageSize, ItemCount - start);

            IList page = new ArrayList();

            //Add the relevant items from the datasource
            for (int i = 0; i < size; i++)
                page.Add(dataSource[start + i]);

            //set the base objects datasource
            base.DataSource = page;
            base.OnDataBinding(e);
        }

        public event DataGridPageChangedEventHandler PageIndexChanged;

        protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
            if (PageIndexChanged != null)
                PageIndexChanged(this, e);
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.