探知,不断发现
探知不断发现
paging control code
  1 [ToolboxData("<{0}:Paging runat=server></{0}:Paging>")]
  2    public class Paging : WebControl
  3    {
  4        public int Records
  5        {
  6            get
  7            {
  8                object o = this.ViewState["Records"];
  9                if (o == null)
 10                {
 11                    return 1;
 12                }

 13                else
 14                {
 15                    return (int)o;
 16                }

 17            }

 18            set
 19            {
 20                this.ViewState["Records"= value;
 21            }

 22        }

 23
 24        public int PageSize
 25        {
 26            get
 27            {
 28                object o = this.ViewState["PageSize"];
 29                if (o == null)
 30                {
 31                    return 1;
 32                }

 33                else
 34                {
 35                    return (int)o;
 36                }

 37            }

 38            set
 39            {
 40
 41                this.ViewState["PageSize"= value;
 42            }

 43        }

 44
 45       
 46        public int ToPage
 47        {
 48            get
 49            {
 50                if (DesignMode)
 51                {
 52                    return 1;
 53                }

 54                string s = Page.Request[PageName];
 55                if (s == null)
 56                {
 57                    return 1;
 58                }

 59                else
 60                {
 61                    return int.Parse(s);
 62                }

 63            }

 64        }

 65
 66        public int PageCount;
 67
 68        public string PageName = "Topage";
 69
 70        
 71        protected override void RenderContents(HtmlTextWriter output)
 72        {
 73            Build(output,CalculatePageCount);
 74        }

 75
 76        private int CalculatePageCount(int records,int pageSize)
 77        {
 78            int mod = records % pageSize;
 79            if (mod > 0)
 80            {
 81                return (records / pageSize + 1);
 82            }

 83            else
 84            {
 85                return (records / pageSize);
 86            }

 87        }

 88
 89        public delegate int CalculatePageCountHandel(int records, int pageSize);
 90
 91        private void Build(HtmlTextWriter output,CalculatePageCountHandel kk)
 92        {
 93            PageCount = kk(Records,PageSize);
 94            string prev = "";
 95            string next = "";
 96            
 97            if (ToPage == 1)
 98            {
 99                prev = "prev ";
100            }

101            else
102            {
103                prev = string.Format("<a href=\"{0}\">prev</a> ", GetUrl(ToPage - 1));
104            }

105            if (ToPage >= PageCount)
106            {
107                next = "next";
108            }

109            else
110            {
111                next = string.Format("<a href=\"{0}\">next</a> ", GetUrl(ToPage + 1));
112            }

113            string temp = "";
114            int previousPagesCount = 3;
115            for (int i = ToPage - previousPagesCount; i < ToPage; i++)
116            {
117                if (i > 0)
118                {
119                    temp += string.Format("<a href=\"{0}\">{1}</a> ", GetUrl(i), i);
120                }

121            }

122            temp += string.Format("<a href=\"{0}\" class=\"selected\">{1}</a> ", GetUrl(ToPage), ToPage);
123            int nextPagesCount = 3;
124            for (int i = ToPage + 1; i <= PageCount && i <= ToPage + nextPagesCount; i++)
125            {
126
127                temp += string.Format("<a href=\"{0}\">{1}</a> ", GetUrl(i), i);
128            }

129            if (ToPage + 1 + 4 < PageCount)
130            {
131                temp += " ";
132            }

133            temp = "<span>" +prev + temp + next+"</span>";
134
135
136            output.Write(temp);
137        }

138        public NameValueCollection ExtendProperty;
139        private string GetUrl(int number)
140        {
141            if (this.DesignMode)
142            {
143                return string.Empty;
144            }

145            NameValueCollection list = new NameValueCollection(Page.Request.QueryString);
146            if (list[PageName] == null)
147            {
148                list.Add(PageName, number.ToString());
149            }

150            else
151            {
152                list[PageName] = number.ToString();
153            }

154            string key;
155            if (ExtendProperty != null)
156            {
157                for (int i = 0; i < ExtendProperty.Count; i++)
158                {
159                    key = ExtendProperty.Keys[i];
160                    if (list[key] == null)
161                    {
162                        list.Add(key, ExtendProperty[key]);
163                    }

164                    else
165                    {
166                        list[key] = ExtendProperty[key];
167                    }

168                }

169            }

170            string url = Page.Request.Path+"?";
171            for (int i = 0; i < list.Count; i++)
172            {
173                url +=  list.Keys[i] + "=" +HttpUtility.UrlEncode(list[i]);
174                if (i != list.Count - 1)
175                {
176                    url += "&";
177                }

178            }

179            return url;
180            
181        }

182    }

pageSize control
 [ToolboxData("<{0}:PagesizeControl runat=server></{0}:PagesizeControl>")]
    
public class PagesizeControl : WebControl
    
{
        
private delegate string PagesizeHtmlHandle(int[] list, int current,GetItemHtmlHandle itemHandle);
        
private delegate string GetItemHtmlHandle(int value, int current);
        
protected override void RenderContents(HtmlTextWriter output)
        
{
            Build(output,GetHtml,GetItemHtml);
        }


        
public int Current
        
{
            
get
            
{
                
if (DesignMode)
                
{
                    
return 50;
                }

                
string s = Page.Request[Name];
                
if (s == null)
                
{
                    
return 50;
                }

                
else
                
{
                    
return int.Parse(s);
                }

            }

        }


        
private const string Name = "pagesize";

        
private void Build(HtmlTextWriter output,PagesizeHtmlHandle handle,GetItemHtmlHandle itemHandle)
        
{
            
int[] list = new int[] {20,50,100};
           
            output.Write(handle(list,Current,itemHandle));  
        }


        
private string GetHtml(int[] list, int current,GetItemHtmlHandle itemHandle)
        
{
            
string temp = "<span>show ";
            
for (int i = 0; i < list.Length; i++)
            
{
                temp 
+= itemHandle(list[i], current);
            }

            temp 
+= "results</span>";
            
return temp;
        }

        
public NameValueCollection ExtendProperty;
        
private string GetItemHtml(int value, int current)
        
{
            
if (DesignMode)
            
{
                
return value.ToString() +" ";
            }

            NameValueCollection list 
= new NameValueCollection(Page.Request.QueryString);
            
if (list[Name] == null)
            
{
                list.Add(Name, value.ToString());
            }

            
else
            
{
                list[Name] 
= value.ToString();
            }

            
string key;
            
if (ExtendProperty != null)
            
{
                
for (int i = 0; i < ExtendProperty.Count; i++)
                
{
                    key 
= ExtendProperty.Keys[i];
                    
if (list[key] == null)
                    
{
                        list.Add(key, ExtendProperty[key]);
                    }

                    
else
                    
{
                        list[key] 
= ExtendProperty[key];
                    }

                }

            }

            
string temp;
            
if (value == current)
            
{
                temp 
= "<a href=\"{0}\" class=\"selected\">{1}</a> ";
            }

            
else
            
{
                temp 
= "<a href=\"{0}\">{1}</a> ";
            }


            
string url = Page.Request.Path + "?";
            
            
for (int i = 0; i < list.Count; i++)
            
{
                url 
+= list.Keys[i] + "=" + HttpUtility.UrlEncode(list[i]);
                
if (i != list.Count - 1)
                
{
                    url 
+= "&";
                }

            }

            
return string.Format(temp, url, value);
        }

    }

On Make url function , You can replace some code
posted on 2008-05-07 10:20  lovebanyi  阅读(351)  评论(0编辑  收藏  举报