ASP.NET2.0中datalist仿百度分页 注意这个分页在1.0中无效 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 MySql.Data.MySqlClient; using System.Data.SqlClient; using System.IO; public partial class mysql : System.Web.UI.Page { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]); int ToatalCountRecord;//总记录数 int PageItem = 4;//每页显示的条数 int CurrentPage = 1;//当前页数 protected void Page_Load(object sender, EventArgs e) {
if (!this.Page.IsPostBack) { if (Request.QueryString["page"] != null) { if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage)) { Response.Write("请输入分页参数!"); Response.End(); return; } }
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds;
objPds.AllowPaging = true;
objPds.PageSize = 10;
int CurPage;
//double temp = dt.Rows.Count / objPds.PageSize;
int PageCount = dtApp.Rows.Count / objPds.PageSize;
if (dtApp.Rows.Count % objPds.PageSize > 0)
{
PageCount++;
} if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
} objPds.CurrentPageIndex = CurPage - 1;
lblCurrentPage.Text = "当前页:" + CurPage.ToString() + " 共" + PageCount.ToString() + "页";
LnkFirst.NavigateUrl = Request.CurrentExecutionFilePath;
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); if (!objPds.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1); LnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageCount);
Dl_Brand.DataSource = objPds;
Dl_Brand.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
HyperLink hy1 = (HyperLink)Dl_Brand.Items[i].FindControl("Hy_Title");
ImageButton logo = (ImageButton)Dl_Brand.Items[i].FindControl("Img_Btn_Logo");
hy1.Text = ds.Tables[0].Rows[i][1].ToString();
hy1.NavigateUrl = "BrandView.aspx?BrandId=" + ds.Tables[0].Rows[i][0].ToString();
//获取图片地址
brand.BrandTypeId = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());
logo.ImageUrl = "~/" + brand.GetBrandLogoUrl();
logo.PostBackUrl = hy1.NavigateUrl.ToString();
}
}
//{
// for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
// {
// HyperLink hy1 = (HyperLink)Dl_Brand.Items[i].FindControl("Hy_Title");
// ImageButton logo = (ImageButton)Dl_Brand.Items[i].FindControl("Img_Btn_Logo");
// hy1.Text = ds.Tables[0].Rows[i][1].ToString();
// hy1.NavigateUrl = "BrandView.aspx?BrandId=" + ds.Tables[0].Rows[i][0].ToString();
// //获取图片地址
// brand.BrandTypeId = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());
// logo.ImageUrl = "~/" + brand.GetBrandLogoUrl();
// logo.PostBackUrl = hy1.NavigateUrl.ToString();
// }
//}
上面的代码我注销掉这些 可以正常分页,在cs里面绑定数据到页面控件就出错.在页面里面用Evla("字段")就不会出错 天!!!!
怎么解决 那位大仙 知道????
=====================================================================
using System;
using System.Data;
using System.Data.SqlClient;
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 DataList_Pageing2 : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds; public int Pagesize
{
get
{
return Convert.ToInt32(Session["page"]);
}
set
{
Session["page"] = value;
}
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int n=0;
if (Request.QueryString["page"] != "")
{
n = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
n = 0;
}
Bind(n);
}
} public void Bind(int n)
{
this.DataList1.DataSource = pds(n);
this.DataList1.DataBind();
} public DataSet GetData(string sql)
{
con = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
da = new SqlDataAdapter(sql,con);
ds = new DataSet();
da.Fill(ds);
return ds;
} public PagedDataSource pds()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GetData("select * from authors").Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = Pagesize;
return pds;
} public PagedDataSource pds(int n)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GetData("select * from authors").Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = Pagesize;
pds.CurrentPageIndex = n;
return pds;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
for (int i = 0; i < pds().PageCount; i++)
{
HyperLink hp = new HyperLink();
Literal li = new Literal(); int n = i + 1;
hp.ID = n.ToString();
hp.Text = n.ToString();
hp.NavigateUrl = "?page=" + i.ToString();
li.Text = " ";
ph.Controls.Add(hp);
ph.Controls.Add(li);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["page"] = Convert.ToInt32(TextBox1.Text);
Bind(0);
string str = Request.Url.ToString().Substring(0,21);
Response.Write(str);
}
}
============================================
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Style="position: relative" RepeatDirection="Horizontal" CellPadding="4" ForeColor="#333333" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table style="width: 100px; position: relative">
<tr>
<td>
<asp:Label ID="lblauid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"au_id") %>'></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblaufname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"au_fname") %>'></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblaucity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"city") %>'></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="lblauzip" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"zip") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</FooterTemplate>
</asp:DataList></div>
<asp:TextBox ID="TextBox1" runat="server" Style="position: relative"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="position: relative"
Text="Button" />
</form>
</body>
</html>
注意这个分页在1.0中无效
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 MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.IO;
public partial class mysql : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]);
int ToatalCountRecord;//总记录数
int PageItem = 4;//每页显示的条数
int CurrentPage = 1;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (Request.QueryString["page"] != null)
{
if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage))
{
Response.Write("请输入分页参数!");
Response.End();
return;
}
}
this.BuidGrid();
}
}
private void BuidGrid()
{
string s2 = "select * from ts1";
SqlDataAdapter da = new SqlDataAdapter(s2, conn);
DataSet ds = new DataSet();
int startRecord = (CurrentPage - 1) * PageItem;
da.Fill(ds, startRecord, PageItem, "a");
this.DataList1.DataSource = ds.Tables["a"].DefaultView;
this.DataList1.DataBind();
SqlCommand comm = new SqlCommand("select count(*) from ts1", conn);
conn.Open();
ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
BuildPages();
}
private void BuildPages()
{
int Step = 5;//偏移量
int LeftNum = 0;//做界限
int RightNum = 0;//右界限
string PageUrl = Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (CurrentPage > 1)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + " </a>";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += " <font color=red>" + " " +"["+i.ToString() +"]"+ "" + " </font>";
}
else
{
OutPut += " <a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + " </a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + " </a>";
}
this.PageInfo.InnerHtml = OutPut;
}
}
请注意颜色标注部分
需要在前台添加一个 <div id="PageInfo" runat="server" >
或者
pagedatasource