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;
using System.Data.OleDb;

public partial class TuiGuang : BasePage
{
     
    protected void Page_Load(object sender, EventArgs e)
    {
        string page = Request.QueryString["page"];
        string u = Request.QueryString["url"];
        if (string.IsNullOrEmpty(u)||string.IsNullOrEmpty(page))
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('参数输入错误！');location.href='http://tool.13sb.cn'</script>");
        }
        if (!string.IsNullOrEmpty(page) & !IsPostBack)
        {
            GetUrl(page);
            this.Title = string.Format("第{0}页-网址批量提交",page);
        }
    }
    int pageCount = 0;
    void GetUrl(string page)
    {
        int p = 30;
        string sql = "";
        if (!string.IsNullOrEmpty(page))
        {
            if (int.Parse(page) == 1)
                sql = "SELECT TOP 30 SITEURL FROM SITE ";
            else
                sql = string.Format("SELECT TOP 30 * FROM SITE WHERE  ID NOT IN(SELECT TOP {0} ID FROM SITE ORDER BY ID ) ORDER BY ID", (int.Parse(page) - 1) * p);
        }
        else
            sql = "SELECT TOP 30 SITEURL FROM SITE ";

        OleDbDataReader reader = null;
        try
        {

            if (int.Parse(page) == 1)
            {
                string countSql = "SELECT COUNT(ID) FROM SITE ";
                object c = SqlHelper.GetSingle(countSql);
                pageCount = Convert.ToInt32(c) % 30 == 0 ? Convert.ToInt32(c) / 30 : Convert.ToInt32(c) / 30 + 1;
                Session["pageCount"] = pageCount.ToString();
            }
            else
            {
                if (Session["pageCount"] != null)
                    pageCount = int.Parse(Session["pageCount"].ToString());
                else
                    pageCount = 1;
            }
            if (int.Parse(page) <= pageCount)
            {
                reader = SqlHelper.ExecuteReader(sql, null);
                string search = "";

                while (reader.Read())
                {
                    string url = reader["SITEURL"].ToString();

                    if (!string.IsNullOrEmpty(Request.QueryString["page"]) && !string.IsNullOrEmpty("url"))
                        search = Request.QueryString["url"];
                    string urlNew = url.Replace("$1", search);
                    this.divUrl.InnerHtml += urlNew + "<br>";
                    this.divUrl.InnerHtml += "<IFRAME marginWidth=0 marginHeight=0 src=\"3ed3e72d-c305-4e06-b338-1dadd34c83e0.htm?" + urlNew + "\" frameBorder=0 width=\"100%\" scrolling=no height=50></IFRAME>";
                }
                int _page = int.Parse(page); _page++;
                string script = "<script>setTimeout('location=\"?url=" + search + "&page=" + _page + "\"',20000);</script>";
                this.RegisterClientScriptBlock(Guid.NewGuid().ToString(), script);
                if (_page >= int.Parse(Session["pageCount"].ToString()))
                    this.HyperLink1.NavigateUrl = "default.aspx";
                this.HyperLink1.Text = "如果20秒没有自动跳转，您可以点这里下一页";
                this.HyperLink1.NavigateUrl = "TuiGuang.aspx?page=" + _page + "&url=" + search;
                this.span2.InnerHtml = "20秒后自动翻页，当前第<font color='red'>" + page + "</font>页,共有<font color='red'>" + pageCount + "</font>页";
            }
            else
            {
                this.HyperLink1.Text = "全部提交完成！";
                this.HyperLink1.NavigateUrl = "default.aspx";
                this.RegisterClientScriptBlock(Guid.NewGuid().ToString(), "<script>setTimeout('location=\"default.aspx\"',10000)</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("err.aspx?s=" +Server.HtmlDecode(ex.Message));
        }
        finally
        {
            if(reader!=null)reader.Close();
        }
    }
}

