using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Tool_pagion : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void GetString(string idColumn, int CurrentPage, int ShowRow, string tableName, string sqlWhere, string PageName, string PageUrl)
    {
        string sql = "";
        string countSql = "SELECT COUNT(*) FROM {0} WHERE {1}";
        countSql = string.Format(countSql, tableName, sqlWhere);
        int count = (int)SqlHelper.GetSingle(countSql);


        //select top 2 * from houseinfo where id not in(select top 1 id from houseinfo order by id desc)order by id desc 

        string sqlStr = string.Format(sql, (CurrentPage - 1) * ShowRow, tableName, idColumn, sqlWhere);

        this.pagion1.InnerHtml = ShowPage(PageName, count, ShowRow, CurrentPage, "条", true, true, true, true, PageUrl);
    }

    private string ShowPage(string mStrFileName, int mTotalput, int mMaxPerPage, int mCurrentPage, string strUnit, bool ShowTotal, bool ShowPageTotal, bool ShowAllPages, bool ShowNumList, string Pageurl)
    {
        if (mTotalput == 0)
            return "";
        int n;
        string strTemp;
        string strUrl;
        if (mTotalput % mMaxPerPage == 0)
            n = mTotalput / mMaxPerPage;
        else
            n = mTotalput / mMaxPerPage + 1;
        if (n == 1)
            return "";
        strTemp = "";
        if (ShowTotal)
            strTemp = strTemp + "共 <b>" + mTotalput + "</b> " + strUnit + "&nbsp;&nbsp;";

        strUrl = JoinChar(mStrFileName);
        if (mCurrentPage < 2)
        {
            strTemp += "首页&nbsp;上一页&nbsp;";
        }
        else
        {
            strTemp += "<a href='" + strUrl + Pageurl + "'>首页</a>&nbsp;";
            strTemp += "<a href='" + strUrl + Pageurl + (mCurrentPage - 1) + "'>上一页</a>&nbsp;";
        }
        if (ShowNumList)
        {
            if (n <= 10)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (mCurrentPage == i)
                    {
                        strTemp += "<font color=red><b>" + i + "</b></font> ";
                    }
                    else
                    {
                        strTemp += "<a href=" + strUrl + Pageurl + i+">" + i + "</a> ";
                    }
                }
            }
            else
            {
                if (mCurrentPage < 5)
                {
                    for (int i = 1; i <= 10; i++)
                    {
                        if (mCurrentPage == i)
                        {
                            strTemp += "<font color=red><b>" + i + "</b></font> ";
                        }
                        else
                        {
                            strTemp += "<a href=" + strUrl + Pageurl + i+">" + i + "</a> ";
                        }
                    }
                }
                else if (n - mCurrentPage < 5)
                {
                    for (int i = n - 10 + 1; i <= n; i++)
                    {
                        if (mCurrentPage == i)
                        {
                            strTemp += "<font color=red><b>" + i + "</b></font> ";
                        }
                        else
                        {
                            strTemp += "<a href=" + strUrl + Pageurl + i+">" + i + "</a> ";
                        }
                    }
                }
                else
                {
                    for (int i = mCurrentPage - 4; i <= mCurrentPage + 5; i++)
                    {
                        if (mCurrentPage == i)
                        {
                            strTemp += "<font color=red><b>" + i + "</b></font> ";
                        }
                        else
                        {
                            strTemp += "<a href=" + strUrl + Pageurl + i + ">" + i + "</a> ";
                        }
                    }
                }
            }
        }
        if (n - mCurrentPage < 1)
        {
            strTemp = strTemp + "下一页&nbsp;尾页";
        }
        else
        {
            strTemp = strTemp + "<a href='" + strUrl + Pageurl + (mCurrentPage + 1) + "'>下一页</a>&nbsp;";
            strTemp = strTemp + "<a href='" + strUrl + Pageurl + n + "'>尾页</a>";
        }
        if (ShowPageTotal)
        {
            strTemp = strTemp + "&nbsp;页次：<strong><font color=red>" + mCurrentPage + "</font>/" + n + "</strong>页 ";
            strTemp = strTemp + "&nbsp;<b>" + mMaxPerPage + "</b>" + strUnit + "/页";
        }
        if (ShowAllPages)
        {
            strTemp = strTemp + "&nbsp;转到：<select name='page' size='1'  align='absmiddle' onchange=javascript:window.location='" + strUrl + Pageurl + "'+this.options[this.selectedIndex].value;>";
            for (int i = 1; i <= n; i++)
            {
                strTemp = strTemp + "<option value='" + i + "'";
                if (mCurrentPage == i) { strTemp = strTemp + " selected "; }
                strTemp = strTemp + ">第" + i + "页</option>";
            }
            strTemp = strTemp + "</select>";
        }
        return strTemp;
    }

    private static string JoinChar(string strUrl)
    {
        if (strUrl == null || strUrl == "") return null;

        if (strUrl.IndexOf('?') < strUrl.Length)
        {
            if (strUrl.IndexOf('?') > 1)
            {
                if (strUrl.IndexOf('&') < strUrl.Length)
                    return strUrl + "&";
                else
                    return strUrl;
            }
            else
                return strUrl;
        }
        else
            return strUrl;
    }
}
