我想把后台传过来的内容 在一个ASPX页面分成几页显示 比如上一页 下一页 之类的
解决方案 »
- ***********运行效率问题*************
- 正则表达式求教 匹配非
- 请教PDF转SWF翻页效果的实现。
- asp.net上传,数据库文件在客户端
- 帮个忙吧,被jQuery折腾死了
- 面向对象设计
- 关于id的问题
- 怎么访问DataGrid里的控件??
- 可能不是一般ORA-01400错误: 无法将 NULL 插入 ("SYZXDB"."HWGPFXFTZL"."SYS_C00001_04032119:46:44$")
- 统计点击次数及在线时长
- 类型问题!SQL中的MONEY类型,在asp.net中什么类型与它是匹配的?????????
- 高分求教!asp.net 在创建自定义用户控件时 网站调试运行成功 但是生成网站出现错误
如果都放在一页 会把页面拉长 效果不好 所以我想实现类似HTML的分页
{
AccessDataSource ads = new AccessDataSource();
ads.SelectCommand = "SELECT * FROM [Topic] ORDER BY [time] DESC";
ads.DataFile = "~/Data/DataBase.mdb";
DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dv;
objPage.AllowPaging = true;
objPage.PageSize = 5;
int TolPage;
TolPage = objPage.PageCount;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
lblCurPage.Text = "目前在第 " + CurPage.ToString() + " 页,共 " + TolPage.ToString() + " 页";
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TolPage.ToString();
if (!objPage.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
//lnkPrev.Visible = false;
//lnkFirst.Visible = false;
}
if (!objPage.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
//lnkNext.Visible = false;
//lnkLast.Visible = false;
}
DataList1.DataSource = objPage;
DataList1.DataBind();
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wodeziyou111/archive/2009/12/14/5003402.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private string ConnectionString = @"server=.;uid=sa;pwd=000000;database=NorthWind";
private System.Text.StringBuilder output = new StringBuilder();
private DataSet BindPageData(int CurrentPage, int PageSize, string SqlCommandText)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SYS_PageDate";
cmd.Parameters.AddWithValue("@sqlstr", SqlCommandText).SqlDbType = SqlDbType.NVarChar;
cmd.Parameters.AddWithValue("@currentpage", CurrentPage).SqlDbType = SqlDbType.Int;
cmd.Parameters.AddWithValue("@pagesize", PageSize).SqlDbType = SqlDbType.Int; DataSet ds = new DataSet();
using (SqlDataAdapter dapter = new SqlDataAdapter(cmd))
{
dapter.Fill(ds);
}
return ds;
}
}
void SelectPager(int PageIndex, int PageCount, int TotalRecord)
{
int previous = 1;
int next = 0;
int step = 5;
int start, end;
if (PageIndex < PageCount)
{
next = PageIndex + 1;
}
else
{
PageIndex = PageCount;
previous = PageCount - 1;
next = PageCount;
} start = 1;
end = PageIndex + step;
if (PageIndex > step)
{
start = PageIndex - step;
}
if (end > PageCount)
{
end = PageCount;
}
else
{
end = end - 1;
}
output.Append("<div class=\"selectpage\">");
if (PageIndex > start)//如果不是首页
{
previous = PageIndex - 1;
output.AppendFormat("<a href=\"{0}?page=1\">首页</a>", Request.FilePath);
output.AppendFormat("<a href=\"{0}?page={1}\">上一页</a>", Request.FilePath, previous);
}
for (int i = start; i <= end; i++)
{
if (PageIndex == i)
{
output.AppendFormat("<span class=\"selectpageindex\"> {0} </span>", i);
}
else
{
output.AppendFormat("<a href=\"{0}?page={1}\" class=\"pageindex\"> {1} </a>", Request.FilePath, i);
}
}
if (PageIndex < end)
{
output.AppendFormat("<a href=\"{0}?page={1}\">下一页</a>", Request.FilePath, next);
output.AppendFormat("<a href=\"{0}?page={1}\">尾页</a>", Request.FilePath, PageCount);
}
output.AppendFormat("当前第<font color=red>{0}</font>页 共<font color=red>{1}</font>页 <font color=red>{2}</font>条数据", PageIndex, PageCount, TotalRecord);
output.Append("</div>");
} string ShowData(int PageIndex)
{ string Sql = @"select * from Customers";
DataSet ds = BindPageData(PageIndex, 15, Sql);
DataTable dt;
if (ds.Tables.Count == 0)
{
dt = ds.Tables[0];
}
else
{
dt = ds.Tables[2];
if (dt.Rows.Count == 0 && PageIndex > 1)//如果输入的当前页数超出总页数
{
ds = BindPageData(Convert.ToInt32(ds.Tables[1].Rows[0][0]), 15, Sql);
dt = ds.Tables[2];
}
}
dt.Columns.Remove("ROWSTAT");
SelectPager(PageIndex, Convert.ToInt32(ds.Tables[1].Rows[0][0]), Convert.ToInt32(ds.Tables[1].Rows[0][1]));
output.Append("<table class=\"tb\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" >");
output.Append("<tr class=\"tb_Header\">");
foreach (DataColumn column in dt.Columns)
{
output.Append("<td>" + column.ColumnName + "</td>");
}
output.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
output.Append("<tr class=\"tb_Content\">");
for (int i = 0; i < dt.Columns.Count; i++)
{
output.Append("<td>");
output.Append(row[dt.Columns[i].ColumnName]);
output.Append("</td>");
}
output.Append("</tr>");
}
output.Append("</table>");
return output.ToString();
}
protected override void OnUnload(EventArgs e)
{
this.output = null;
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
a:link
{
font-size: 9pt;
color:#3E94F1;
text-decoration: none;
}
a:visited
{
color:#000fff;
text-decoration: none;
}
a:hover
{
color: red; text-decoration: underline
}
a:active
{
text-decoration: none
}
.selectpage{font-size:9pt;color:#3E94F1;width:100%;text-align:center}
.selectpageindex{color:#fff;background-color:#3E94F1;padding:1px;width:20px;margin-right:5px}
.pageindex{border:solid 1px #ccc;padding:1px;margin-right:7px}
.tb{width:100%;border-collapse:collapse}
.tb_Header{font-size:12px;font-weight:bold;color:#3E94F1;texta-align:center}
.tb_Content{font-size:9pt;color:#3E9345}
body{margin:0px}
ul{margin:0px}
ul li{margin:0px;display:inline;}
</style>
</head>
<body>
<form id="form1" runat="server">
<%=ShowData(Request.QueryString["page"] == null ? 1 : Convert.ToInt32(Request.QueryString["page"])) %>
</form>
</body>
</html>
CREATE PROCEDURE SYS_PageDate
(
@sqlstr nvarchar(4000),
@currentpage int, --頁次
@pagesize int --行數
)
AS
set nocount on
DECLARE
@P1 int, --P1是游標的id
@rowcount int SET @sqlstr=replace(replace(replace(replace(replace(replace(replace(replace(@sqlstr,'exec ',''),'insert ',''),'delete ',''),'update ',''),'drop ',''),'alter ',''),'chr(',''),'mid(','')exec sp_cursoropen @P1 output ,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output -- 頁數 行數
SELECT ceiling(1.0*@rowcount/@pagesize) AS PageCount,@rowcount AS RowCounts --as ?行?,@currentpage as
set @currentpage=(@currentpage-1)*@pagesize+1 exec sp_cursorfetch @P1,16,@currentpage,@pagesize exec sp_cursorclose @P1 set nocount off
GO
建议用SQL分页,取得一页的资料,这样页面效率比较好。
网上查一下 一条SQL搞定分页
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.Cookies["kk"].Value;
if (!Page.IsPostBack)
{
bind();
int n = Convert.ToInt16(this.Label3.Text);//总页数标签
int i;
for (i = 1; i <= n; i++)
DropDownList1.Items.Add(Convert.ToString(i));
// 下拉列表
}
}
protected void bind()
{ //取得当前页的页码
int curpage = Convert.ToInt32(this.LinkButton1.Text);//当前页,给初值
//使用PagedDataSource类实现DataList控件的分页功能
PagedDataSource ps = new PagedDataSource();
//获取数据集
DB dbc = new DB();
DataSet ds = dbc.getdatafromdb("select n_title,n_author,n_writerdate,n_id from [news] where lbid in(select lbid from newslb where lbname='" + Label1.Text + "')");
ps.DataSource = ds.Tables[0].DefaultView;
//是否可以分页
ps.AllowPaging = true;
//显示的数量
ps.PageSize = 1;
//取得当前页的页码
ps.CurrentPageIndex = curpage - 1;
this.LinkButton2.Enabled = true;
this.LinkButton3.Enabled = true;
this.LinkButton5.Enabled = true;
this.LinkButton4.Enabled = true;
if (curpage == 1)
{
//不显示第一页按钮
this.LinkButton4.Enabled = false;
//不显示上一页按钮
this.LinkButton2.Enabled = false;
}
if (curpage == ps.PageCount)
{
//不显示下一页
this.LinkButton3.Enabled = false;
//不显示最后一页
this.LinkButton5.Enabled = false;
}
//显示分页数量
this.Label3.Text = Convert.ToString(ps.PageCount);
//绑定DataList控件
this.GridView1.DataSource = ps;
// this.dlNews.DataKeyNames= "id";
this.GridView1.DataBind();
dbc.clear(); }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
LinkButton1.Text = DropDownList1.SelectedValue;
this.bind();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
LinkButton1.Text = "1";
this.bind();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
LinkButton1.Text =Label3.Text;
this.bind(); }
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton1.Text = Convert.ToString(Convert.ToInt32(this.LinkButton1.Text) - 1);
this.bind();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
LinkButton1.Text = Convert.ToString(Convert.ToInt32(this.LinkButton1.Text) + 1);
this.bind();
}
{
string Output;
string ThisPage = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"]; string PageNo;
PageNo = HttpContext.Current.Request.QueryString["Page"]; if (Article.IndexOf("pagebreak") != -1)
{
string[] Pages = Regex.Split(Article, "pagebreak");
int TotalPages = Pages.GetUpperBound(0) + 1;
int Counter = 1;
int PageNumber = 1;
if (PageNo != null)
{
PageNumber = System.Convert.ToInt32(PageNo);
}
Output = Pages[PageNumber - 1];
Output += "<p>Go to Page ";
while (Counter <= TotalPages)
{
if (Counter == PageNumber)
{
Output += Counter.ToString() + " ";
}
else
{
Output += "<a href=\"" + ThisPage + "?Page=" + Counter.ToString();
Output += "\">" + Counter.ToString() + "</a> ";
}
Counter++;
}
Output += "</p>";
}
else
{
Output = Article;
}
return Output;
}如有不足 请大家谅解哈
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace yournamespace
{
/// <summary>
/// 文本自动分页控件
/// </summary>
[ToolboxData("<{0}:PagerText runat=\"server\"></{0}:PagerText>")]
public class PagerText : WebControl
{ private string inputText;
/// <summary>
/// 设置或获取输入文本
/// </summary>
public string InputText
{
get { return inputText; }
set { inputText = value; }
} /// <summary>
/// 返回输出文本
/// </summary>
public string OutputText
{
get
{ HttpRequest request = HttpContext.Current.Request;
if (String.IsNullOrEmpty(request[pageQueryKey]))//判断传递的页数是否为空,如为空则设启始页为1
{
currentPage = 1;
}
else//否则为接受页数
{
currentPage = Convert.ToInt32(request[pageQueryKey]);//将接受页数转换为Int型
} pageCount = (int)Math.Ceiling((double)inputText.Length / pageSize); if (currentPage > pageCount) currentPage = pageCount;
if (currentPage < pageCount)
{ return inputText.Substring((currentPage - 1) * pageSize, pageSize);
}
else
{
return inputText.Substring((currentPage - 1) * pageSize);
}
}
} int pageCount = 1;
/// <summary>
/// 返回页面总数
/// </summary>
public int PageCount
{
get
{
return pageCount;
} } int currentPage = 1;
/// <summary>
/// 返回当前页
/// </summary>
public int CurrentPage
{
get
{
return currentPage; ;
} } private int pageSize = 500;
/// <summary>
///设置或获取每页显示大小
/// </summary>
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
} private string pageQueryKey = "pp"; /// <summary>
/// 设置或获取查询关键字的名称
/// </summary>
public string PageQueryKey
{
get { return pageQueryKey; }
set { pageQueryKey = value; }
} private string nextText = "下页";
/// <summary>
/// 设置或获取下一页的提示文字
/// </summary>
public string NextText
{
get { return nextText; }
set { nextText = value; }
} private string preText = "上页";
/// <summary>
/// 设置或获取上一页的提示文字
/// </summary>
public string PreText
{
get { return preText; }
set { preText = value; }
} private bool showPageNumber = false; /// <summary>
/// 设置或获取是否显示分页字符
/// </summary>
public bool ShowPageNumber
{
get { return showPageNumber; }
set { showPageNumber = value; }
} private string spaceSymbol = "  ";
/// <summary>
/// 设置或获取分页间隙符号
/// </summary>
public string SpaceSymbol
{
get { return spaceSymbol; }
set { spaceSymbol = value; }
} /// <summary>
/// 自定义分页URL的委托
/// </summary>
/// <param name="showPageNo"></param>
/// <param name="currentPage"></param>
/// <returns></returns>
public delegate string CustomerLink(int showPageNo, int currentPage); /// <summary>
/// 获取或设置自定义分页的方法
/// </summary>
private CustomerLink formatCustomerLink; public CustomerLink FormatCustomerLink
{
get { return formatCustomerLink; }
set { formatCustomerLink = value; }
} protected override void Render(HtmlTextWriter writer)
{
if (!String.IsNullOrEmpty(inputText))
{
if (pageCount == 1) return; if (currentPage < pageCount)
{
writer.Write("<a href=\"" + FormatLink(currentPage + 1, currentPage) + "\">" + nextText + "</a>" + spaceSymbol);
} for (int j = 1; j <= pageCount; j++)
{
if (j == currentPage)
{
// TODO:支持自定义当前页样式
// writer.Write("<a href=\"#\" class=\"action\">" + j + "</a>" + spaceSymbol);
writer.Write(j + spaceSymbol);
}
else
{
writer.Write("<a href=\"" + FormatLink(j, currentPage) + "\">" + j + "</a>" + spaceSymbol);
} } if (currentPage != 1)
{
writer.Write("<a href=\"" + FormatLink(currentPage - 1, currentPage) + "\">" + preText + "</a>" + spaceSymbol);
}
} } public virtual string FormatLink(int showPageNo, int currentPage)
{
if (FormatCustomerLink != null) return FormatCustomerLink(showPageNo, currentPage); NameValueCollection list = new NameValueCollection(HttpContext.Current.Request.QueryString); list.Remove(pageQueryKey);
list.Add(pageQueryKey, showPageNo.ToString()); string url = HttpContext.Current.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 url;
} }
}