using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using Microsoft.VisualBasic;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Boolean haveRec = false;
int pageSize = 5;
int p =Convert.ToInt32(Request["p"]);
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from test order by id ", conn);
DataSet ds = new DataSet();
da.Fill(ds, "test");
DataTable dt = ds.Tables["test"];
int recordCount = dt.Rows.Count;
if (recordCount == 0)
{
Response.Write("抱歉找不到資料!");
Response.End();
}
int totalPage = ((recordCount + pageSize) - 1) / pageSize;
if (Request["p"] == null )
{
p = 1;
}
else
{
if (IsNumeric(Request["p"]))
{
if((p!=null) && (p>0) && (p<=totalPage))
{
p=Convert.ToInt32(Request["p"]);
}
else
{
p = 1;
}
}
else
{
p = 1;
}
}
int nowRecordPosition = 0;
int rowno=0;
StringBuilder sb = new StringBuilder();
if (p > 0)
{
nowRecordPosition = (p - 1) * pageSize;
}
Response.Write("<h3>搜尋資料庫:共計" + recordCount + "筆/共需" + totalPage + "頁</h3><hr width='90%' size=1>");
sb.Append("<table border=1 width='80%' margin='auto' align='center'");
while (rowno < pageSize && nowRecordPosition < recordCount)
{
haveRec = true;
sb.Append("<tr><td width='10%'><font size=2 color=#800000>★(" + dt.Rows[nowRecordPosition]["test_time"] + ")</font></td><td width='60%'><a href='disp.aspx?id="+dt.Rows[nowRecordPosition]["id"].ToString()+"'>"+ dt.Rows[nowRecordPosition]["title"] +"</a></td></tr>");
sb.Append("<tr><td width='10%'></td><td width='60%'>"+dt.Rows[nowRecordPosition]["summary"]+"</td></tr>");
rowno += 1;
nowRecordPosition += 1;
}
sb.Append("</table>");
Response.Write(sb);
if (totalPage > 0)
{
Response.Write("<div align='center'>");
if (p > 1)
{
Response.Write("<a href='Default7.aspx?p=" + (p - 1) + "'>上一頁</a>");
}
if (p < totalPage)
{
Response.Write("<a href='Default7.aspx?p=" + (p + 1) + "'>下一頁</a>");
}
Response.Write("<hr width='90%'>");
//for (int i = 1; i <= totalPage; i++)
//{
// if (p == i)
// {
// Response.Write("[" + p + "]" + " ");
// }
// else
// {
// Response.Write("<a href='Default7.aspx?p=" + i + "'>" + i + "</a> ");
// }
//}
int block_page = 0;
block_page = p / 10;
if (block_page > 0)
{
Response.Write("<a href='Default7.aspx?p=" + (((block_page - 1) * 10) + 9) + "'>[前十頁<<] </a> ");
}
for (int i = 0; i <= 10; i++)
{
if ((block_page * 10 + i) <= totalPage)
{
if (((block_page * 10) + i) == p)
{
Response.Write("[<b>" + p + "</b>]" + " ");
}
else
{
if ((block_page * 10 + i) != 0)
{
Response.Write("<a href='Default7.aspx?p=" + (((block_page) * 10) +i) + "'>"+((block_page*10)+i)+" </a> ");
}
}
}
}
if(block_page<(totalPage/10) && (totalPage>=((block_page+1)*10+1)))
{
Response.Write("<a href='Default7.aspx?p=" + (((block_page + 1) * 10) + 1) + "'>[>>後十頁] </a> ");
}
Response.Write("</div>");
}
}
static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
bool isNum;
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
double retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
}