.Net 分页实现系列之三---基于有查询条件的分页的方式 (1-13 11:38)
因为有朋友问起,使用ASPNETPager如何进行有查询条件的分页,特意写个测试例子给大家.只供普通程序员参考
WebPage
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!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">
<div>
<a href ="/a/1.aspx">abc</a>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btns" runat="server" OnClick="btns_Click" Text="search" /><br />
<asp:Label ID="lblnewstype" runat="server"></asp:Label><br />
<br />
<br />
<br />
<asp:Label ID="lblContent" runat="server"></asp:Label><br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" />
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanging="AspNetPager1_PageChanging">
</webdiyer:AspNetPager>
</div>
</form>
</body>
</html>
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.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
public partial class Default2 : System.Web.UI.Page
{
private Database db = null;
private DataSet myds = null;
private string strNewsId = "";
private int iPageNo = 1;
private string strNewsTypeName;
private string strconn = "conndb";
private string txt = "";
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Url.AbsoluteUri);
strNewsId ="1";
txt = Request.QueryString["txt"];
bindNewsType();
if (!Page.IsPostBack)
{
AspNetPager1.PageSize = iPageNo;
TextBox1.Text = txt;
GetLog();
}
}
private void bindNewsType()
{
string sql = string.Format("Select * from NewsCat order by pos");
try
{
db = DatabaseFactory.CreateDatabase(strconn);
myds = db.ExecuteDataSet(CommandType.Text, sql);
int iCount = 0;
iCount = myds.Tables[0].Rows.Count;
string str = "";
if (iCount > 0)
{
for (int i = 0; i < iCount; i++)
{
if (myds.Tables[0].Rows[i]["Id"].ToString() == strNewsId)
strNewsTypeName = myds.Tables[0].Rows[i]["Name"].ToString();
str += " ·<a href='newsmore.aspx?NewsId=" + myds.Tables[0].Rows[i]["ID"].ToString() + "' class='b'>" + myds.Tables[0].Rows[i]["Name"].ToString() + "<a/><br>";
}
}
this.lblnewstype.Text = str;
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
}
private void GetLog()
{
string strsql;
//统计总的记录数
try
{
if (txt=="")
strsql = string.Format("Select count(ID) from News where CatID={0}", strNewsId);
else
strsql = string.Format("Select count(ID) from News where CatID={0} and Title like '%{1}%'", strNewsId,txt);
db = DatabaseFactory.CreateDatabase(strconn);
DataSet ds = new DataSet();
ds = db.ExecuteDataSet(CommandType.Text, strsql);
this.AspNetPager1.RecordCount = System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);
//Response.Write(ds.Tables[0].Rows[0][0]);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
bindNewsData();
}
private void bindNewsData()
{
if (strNewsId == null || strNewsId == "")
return;
DataSet ds1 = null;
string strMiddle = "";
string strButtom = "";
string strHead = "";
string sql = "";
strMiddle = "";
strButtom = "";
strHead = @" <table border='0' cellpadding='0' cellspacing='0' width='90%' align='center' bordercolor='#0099cc'> ";
int RecordPage = (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize;
if (txt== "")
{
if (AspNetPager1.CurrentPageIndex == 1)
sql = string.Format("Select Top {0} * From News Where CatID={1} Order By ID Desc", iPageNo, strNewsId);
else
sql = String.Format(" Select Top {0} * From News Where CatID={1} and Id Not in ( Select Top {2} Id From News Where CatID={3} Order By ID Desc) Order By ID Desc", AspNetPager1.PageSize.ToString(), strNewsId, RecordPage.ToString(), strNewsId);
}
else
{
if (AspNetPager1.CurrentPageIndex == 1)
sql = string.Format("Select Top {0} * From News Where CatID={1} and title like '%{2}%' Order By ID Desc", iPageNo, strNewsId,txt);
else
sql = String.Format(" Select Top {0} * From News Where CatID={1} and title like '%{4}%' and Id Not in ( Select Top {2} Id From News Where CatID={3} and title like '%{4}%' Order By ID Desc) Order By ID Desc", AspNetPager1.PageSize.ToString(), strNewsId, RecordPage.ToString(), strNewsId,txt);
}
Response.Write(sql);
strMiddle += @"<tr ><td> <br>◎ " + strNewsTypeName + "<b>";
strMiddle += @"</b>  </td>
<td> </td>
</tr> <tr><td background='images/bg_news.jpg'>";
try
{
db = DatabaseFactory.CreateDatabase(strconn);
ds1 = db.ExecuteDataSet(CommandType.Text, sql);
int NewsCount = ds1.Tables[0].Rows.Count;
for (int j = 0; j < NewsCount; j++)
{
strMiddle += @"<font color='#FF0000'>
<li><a href=newsinfo.aspx?newid=" + ds1.Tables[0].Rows[j]["ID"].ToString() + " target=_blank >" + ds1.Tables[0].Rows[j]["Title"].ToString() + @"</a>
<font size=1> " + ds1.Tables[0].Rows[j]["AddedDate"].ToString() + "</font>";
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
strButtom += "</td></tr></table>";
this.lblContent.Text = strHead + strMiddle + strButtom;
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
bindNewsData();
System.Text.StringBuilder sb = new StringBuilder("<script Language=\"Javascript\"><!--\n");
sb.Append("var el=document.all;");
// sb.Append(this.GridView1.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
if (!ClientScript.IsStartupScriptRegistered("scrollScript"))
ClientScript.IsStartupScriptRegistered(sb.ToString());
}
}