
时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)
1.首先我是用存储过程来解决的,要弄懂这个问题,首先要从存储过程下手,代码如下:
CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600)
set nocount on--关闭计数
set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
execute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT
if (@RecordCount = 0) --如果没有贴子,则返回零
return 0
/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
其实代码也很简单,学编程的人基本上都是懂数据库的,这个存储过程估计不是问题。
其他的代码我都做了解释,有颜色的那段我没有解释,我在这里解释一下。其实也很简单,大家来看:
select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+'
这段代码的执行结果是什么了,是不是当前页前面的主键的集合啊,现在我们从所有的表中选出主键的值不在这个结果的之内的pagesize个记录不就是当前页的内容了吗?
2.aspx页面就不用再将了吧?我这里将代码写上:
<%@ Page Language=&C#& AutoEventWireup=&true& CodeFile=&aa.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&>
<div>
<asp:GridView ID=&GridView1& runat=&server& AutoGenerateColumns=&False& Height=&180px& Width=&867px&>
<Columns>
<asp:BoundField DataField=&job_id& HeaderText=&job_id& />
<asp:BoundField DataField=&job_desc& HeaderText=&job_desc& />
<asp:BoundField DataField=&max_lvl& HeaderText=&max_lxl& />
</Columns>
</asp:GridView>
</div>
<asp:HyperLink ID=&hylfirst& runat=&server&>首页</asp:HyperLink>
<asp:HyperLink ID=&hylprev& runat=&server&>上一页</asp:HyperLink>
<asp:HyperLink ID=&hylnext& runat=&server&>下一页</asp:HyperLink>
<asp:HyperLink ID=&hylend& runat=&server&>尾页</asp:HyperLink>
第<asp:Label ID=&lbRow& runat=&server& Text=&Label&></asp:Label>页,
共<asp:Label ID=&lbpage& runat=&server& Text=&Label&></asp:Label>页,共<asp:Label
ID=&lbRecord& runat=&server& Text=&Label&></asp:Label>条记录,转到<asp:TextBox ID=&txtlink&
runat=&server& Width=&29px&></asp:TextBox>
页<asp:LinkButton ID=&link& runat=&server& OnClick=&link_Click& TabIndex=&1&>转到</asp:LinkButton>
</form>
</body>
</html>
3.cs页面其实也每页什么好讲的,也就是一些常用的代码罢了……我把代码加上,大家看看,要是有疑问的可以回复我再解释:
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 System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.bind();
}
protected void link_Click(object sender, EventArgs e)
{
int page = Convert.ToInt32(txtlink.Text);
Response.Redirect(&aa.aspx?CurrentPage=&+page+&&);
}
public void bind()
{
int sumPage;
int pageNo = 1;
int pageSize = 3;
if (Request.QueryString[&CurrentPage&] == null)
{
pageNo = 1;
}
else
{
pageNo = Int32.Parse(Request.QueryString[&CurrentPage&]);
}
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings[&ConStr&]);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = &getdataset&;
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(&@TableList&, SqlDbType.VarChar, 200).Value = &job_id,job_desc,max_lvl&;
da.SelectCommand.Parameters.Add(&@TableName&, SqlDbType.VarChar, 30).Value = &jobs&;
//da.SelectCommand.Para
关键词标签:ASP.NET
相关阅读 ASP.NET创建XML Web服务全接触 如何提高ASP.NET页面载入速度的方法 .net导出海量数据到execl文件 ASP.NET 2.0程序安全的基础知识 ASP.NET中MD5与SHA1加密的几种方法 在.net开发中几个重要的认识误区
热门文章
在ASP.NET MVC中实现大文件异步上传
在.NET环境下为网站增加IP过滤功能
诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022
ASP.NET 如何避免页面重新整理时重复送出
钟离圣遗物推荐-原神钟离圣遗物词条
人气排行 诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022 asp.net表单提交方法GETPOST 在ASP.NET中如何判断用户IE浏览器的版本 Asp.net中messagebox的实现方法 Asp.net中的web.config配置 在ASP.NET MVC中实现大文件异步上传 asp.net获取URL和IP地址 FileUpload上传多文件出现错误的解决方法 ASP.NET Web.config配置详解 Asp.net常用的51个代码(非常实用) ASP.NET打开word文档出错的解决办法 让你的.NET程序兼容不同版本的Dll文件
查看所有0条评论>>