读书人

用AspNetPager对搜寻结果分页

发布时间: 2011-12-21 23:56:01 作者: rapoo

用AspNetPager对搜索结果分页
我没有分了..

请问要怎么实现!!!

[解决办法]
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace ShowPage
{
/// <summary>
/// Index 的摘要说明。
/// </summary>
public class Index : System.Web.UI.Page
{
protected Wuqi.Webdiyer.AspNetPager pager;
protected System.Web.UI.WebControls.DataGrid dgList;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConn);
string strsql="Select count(id) from article";
SqlCommand myCommand = new SqlCommand(strsql,myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda =new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds = new DataSet();
myda.Fill(ds,"article");
this.pager.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);
BindData();
}

}

void BindData()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConn);
string strsql="Select id,topic from article";
SqlCommand myCommand = new SqlCommand(strsql,myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda =new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds=new DataSet();
myda.Fill(ds,pager.PageSize*(pager.CurrentPageIndex-1),pager.PageSize,"article");
this.dgList.DataSource=ds.Tables["article"];
this.dgList.DataBind();
//动态设置用户自定义文本内容
pager.CustomInfoText="记录总数:<font color=\"blue\"><b>"+pager.RecordCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+pager.PageCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pager.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.pager_PageChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
pager.CurrentPageIndex=e.NewPageIndex;
BindData();
System.Text.StringBuilder sb=new StringBuilder("<script Language=\"Javascript\"><!--\n");
sb.Append("var el=document.all;");


sb.Append(this.dgList.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
if(!Page.IsStartupScriptRegistered("scrollScript"))
Page.RegisterStartupScript("scrollScript",sb.ToString());

}
}
}

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>
<%@Register TagPrefix="Webdiyer" Namespace="Wuqi.Webdiyer" Assembly="aspnetpager"%>
<HTML>
<HEAD>
<TITLE>Welcome to Webdiyer.com </TITLE>
<script runat="server">
SqlConnection conn;
SqlCommand cmd;
void Page_Load(object src,EventArgs e)
{
conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
if(!Page.IsPostBack)
{
cmd=new SqlCommand("GetNews",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",1);
cmd.Parameters.Add("@pagesize",1);
cmd.Parameters.Add("@docount",true);
conn.Open();
pager.RecordCount=(int)cmd.ExecuteScalar();
conn.Close();
BindData();
}
}

void BindData()
{
cmd=new SqlCommand("GetNews",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",pager.CurrentPageIndex);
cmd.Parameters.Add("@pagesize",pager.PageSize);
cmd.Parameters.Add("@docount",false);
conn.Open();
dataGrid1.DataSource=cmd.ExecuteReader();
dataGrid1.DataBind();
conn.Close();
pager.CustomInfoText="记录总数:<font color=\"blue\"><b>"+pager.RecordCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+pager.PageCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";
}
void ChangePage(object src,PageChangedEventArgs e)
{
pager.CurrentPageIndex=e.NewPageIndex;
BindData();
}
</script>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer(yhaili@21cn.com)">
</HEAD>
<body>
<form runat="server" ID="Form1">
<asp:DataGrid id="dataGrid1" runat="server" />

<Webdiyer:AspNetPager id="pager"
runat="server"
PageSize="8"
NumericButtonCount="8"
ShowCustomInfoSection="left"


PagingButtonSpacing="0"
ShowInputBox="always"
CssClass="mypager"
HorizontalAlign="right"
OnPageChanged="ChangePage"
SubmitButtonText="转到"
NumericButtonTextFormatString="[{0}]"/>

</form>
</body>
</HTML>

读书人网 >asp.net

热点推荐