闲着无聊,写了一个分页的数据列表,有写的不对的地方请大虾们指正:)
default.aspx:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="
Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
</form>
</body>
</html>
default.aspx.cs:
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String pageindex = Request["pageindex"];
if (Request["pageindex"] == null || Request["pageindex"].Length == 0)
pageindex = "1";
try
{
int.Parse(pageindex);
}
catch
{
pageindex = "1";
}
dbconnection dbc = new dbconnection();
String html = dbc.getdatalist("customers", "customerid,companyname,contactname,contacttitle", 10, int.Parse(pageindex));
Literal1.Text = html;
if (pageindex == "1")
Literal2.Text = "<a href='default.aspx?pageindex=" + (int.Parse(pageindex) + 1) + "'>下一页</a> 第"+pageindex+"页";
else
Literal2.Text = "<a href='default.aspx?pageindex=" + (int.Parse(pageindex) + 1) + "'>下一页</a><a href='default.aspx?pageindex=" + (int.Parse(pageindex) - 1) + " '>上一页</a> 第"+pageindex+"页";
}
}
dbconnection.cs:
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
/// <summary>
///dbconnection 的摘要说明
/// </summary>
public class dbconnection
{
private SqlConnection conn;
public dbconnection()
{
//
//TODO: 在此处添加构造函数逻辑
//
getConnection();
}
private SqlConnection getConnection()
{
conn = new SqlConnection("server=192.168.0.100;uid=sa;pwd=sa;database=northwind");
return conn;
}
private void openConnection()
{
if (conn != null && conn.State != ConnectionState.Open) conn.Open();
}
private void closeConnection()
{
if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
}
public String getdatalist(String tablename, String fieldlist, int pagesize, int pageindex)
{
StringBuilder sb = new StringBuilder();
String id
= fieldlist.Substring(0, fieldlist.IndexOf(','));
int topn = (pageindex - 1) * pagesize;
String query = String.Format("select top {0} {1} from {2} where {3} not in (select top {4} {3} from {2})", pagesize, fieldlist, tablename, id, topn);
SqlCommand cmd = new SqlCommand(query, conn);
openConnection();
SqlDataReader dr = cmd.ExecuteReader();
sb.Append("<table border=0 cellpadding=0 cellspacing=1><tr style='background-color:grey;color:white;'>");
DataTable dsheaders = dr.GetSchemaTable();
for (int i = 0; i < dsheaders.Rows.Count; i++)
{
sb.Append(String.Format("<th>{0}</th>", dsheaders.Rows[i]["columnname"]));
}
sb.Append("</tr>");
int color = 0;
while (dr.Read())
{
color++;
if (color % 2 == 0) sb.Append("<tr style='background-color:lightblue;'>");
else sb.Append("<tr>");
for (int a = 0; a < dr.FieldCount; a++)
{
sb.Append(String.Format("<td>{0}</td>", dr.GetValue(a)));
}
sb.Append("</tr>");
}
sb.Append("</table>");
closeConnection();
return sb.ToString();
}
}