存储过程:

-- =============================================
-- Author:
-- Create date:
-- Description: 分页
--Update Date:
--增加了默认排序规则,根据主键升序(防止在视图查询中乱号)
-- =============================================
ALTER PROCEDURE [dbo].[getdatabyPageIndex]
@tablename nvarchar(200),
@columns nvarchar(500)='*',
@condition nvarchar(200)='',
@pagesize int=10,
@pageindex int=0,
@pk nvarchar(30),
@total int output, --统计总共的条数
@orderculumn nvarchar(50)=@pk,
@isasc nvarchar(10)='desc' AS
BEGIN
DECLARE @sql nvarchar(2000)
SET @sql='select top '+cast(@pagesize AS nvarchar(10))+' '+@columns+' from '+@tablename+' where '+
@pk+' not in (select top '+cast((@pagesize*@pageindex) AS nvarchar(10))+
' '+@pk+' from '+@tablename +' where 1=1 '+@condition+' order by '+@orderculumn+' '+@isasc+')'+@condition +' order by '+@orderculumn+' '+@isasc
PRINT @sql
EXEC(@sql)
DECLARE @sql2 nvarchar(2000)
SET @sql2='SELECT @total1 = count(*) FROM '+ @tablename+' WHERE 1=1 '+ @condition
EXEC sp_executesql @sql2,N'@total1 int output',@total output END

.cs:

      /// <summary>
/// 分页功能
/// </summary>
/// <param name="tablename">表名</param>
/// <param name="columns">列名</param>
/// <param name="condition">条件,不需要带where</param>
/// <param name="pagesize">每页显示条数</param>
/// <param name="pageindex">页码</param>
/// <param name="pk">主键</param>
/// <returns>DataTable</returns>
public DataTable getdatabyPageIndex(string tablename, string columns, string condition, int pagesize, int pageindex, string pk,out int totalcount,string ordercolumn,string isasc)
{
string order = "";
if (ordercolumn == null)
{
order = pk;
} string asc = "";
if (isasc == null)
{
isasc = "desc";
} SqlParameter[] pars = new SqlParameter[]{
new SqlParameter("@tablename",tablename),
new SqlParameter("@columns",columns),
new SqlParameter("@condition",condition),
new SqlParameter("@pagesize",pagesize),
new SqlParameter("@pageindex",pageindex),
new SqlParameter("@pk",pk),
new SqlParameter("@total",SqlDbType.Int),
new SqlParameter("@orderculumn",ordercolumn),
new SqlParameter("@isasc",isasc)
};
pars[].Direction = ParameterDirection.Output;
DataTable dt= db.ExcuteSelectReturnDataTable("sp_getdatabyPageIndex", CommandType.StoredProcedure, pars);
totalcount= int.Parse(pars[].Value.ToString());
return dt;
} /// <summary>
/// 执行一个Select语句或者相应的存储过程实现返回数据集合DataSet
/// </summary>
/// <param name="SelectStr">执行一个Select语句或者相应的存储过程</param>
/// <param name="type">指定命令类型</param>
/// <param name="pars">相应参数集合</param>
/// <returns>DataSet</returns>
public DataSet ExcuteSelectReturnDataSet(string SelectStr, CommandType type, SqlParameter[] pars)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(ConnString);
SqlDataAdapter sda = new SqlDataAdapter(SelectStr, conn);
if (pars != null && pars.Length > )
{
foreach (SqlParameter p in pars)
{
sda.SelectCommand.Parameters.Add(p);
}
}
sda.SelectCommand.CommandType = type;
sda.Fill(ds);
return ds;
}

.aspx:

    <style type="text/css">
.pages { color: #; width:%; height:25px;}
.pages a, .pages .cpb { text-decoration:none; padding: 5px; border: 1px solid #ddd; background: #ffff;margin: 2px; font-size:12px; color:#; height:20px}
.pages a:hover { background-color: #2F7EAE; color:#fff;border:1px solid #2F7EAE; text-decoration:none;}
.pages .cpb { font-weight: bold; color: #fff; background: #2F7EAE; border:1px solid #2F7EAE; height:20px}
</style> <asp:HiddenField ID="hidden" runat="server" Value=" " /> <asp:Repeater ID="rptProlist" runat="server" onitemdatabound="rptProlist_ItemDataBound">
<HeaderTemplate>
<table border="" class="infolist" cellpadding="" cellspacing="">
<thead>
<tr>
<th width="4%">序号</th>
<th>项目名称</th>
<th width="10%">开始日期</th>
<th width="10%">预计结束日期</th>
<th width="10%">项目状态</th>
<th width="7%">创建人</th>
<th width="7%">负责人</th>
<th width="20%">参与人</th>
<th style="border-right:1px solid #999999;" width="10%">实际结束日期</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("ProjectID") %></td>
<td><a href="projectdetails.aspx?id=<%#Eval("ProjectID") %>"><%#Eval("ProjectName") %></a></td>
<td><%#Eval("StartDate","{0:yyyy-MM-dd}")%></td>
<td><%#Eval("ExpectedEndDate","{0:yyyy-MM-dd}")%></td>
<td><%#Eval("ProjectState")%></td>
<td><%#returnUserRealName(Eval("ProjectCreater").ToString())%></td>
<td><%#returnUserRealName(Eval("ProjectPrincipal").ToString())%></td>
<td><asp:Label ID="lbPlayers" runat="server" Text='<%#Eval("ProjectPlayers") %>'></asp:Label></td>
<td style="border-right:1px solid #999999;">&nbsp;<%#Eval("EndDate","{0:yyyy-MM-dd}")%>&nbsp;</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" firstpagetext="首页" lastpagetext="尾页"
nextpagetext="下一页" numericbuttoncount=""
pageindexboxtype="DropDownList" prevpagetext="上一页"
showcustominfosection="Left" showpageindexbox="Always"
submitbuttontext="Go" textafterpageindexbox="页"
textbeforepageindexbox="转到" AlwaysShow="True" CustomInfoHTML="第%CurrentPageIndex%/%PageCount%页,每页%PageSize%条,共%RecordCount%条信息"
LayoutType="Table" CssClass="pages" CustomInfoClass="paginator" CurrentPageButtonClass="cpb"
Height="30px" CustomInfoSectionWidth="" Wrap="False"
OnPageChanging="AspNetPager1_PageChanging"
Width="660px" onpagechanged="AspNetPager1_PageChanged">
</webdiyer:AspNetPager>

.aspx.cs:

        //string condition = "  ";
//每页条数
int pagesize = ;
//总共条数
int recordCount = ;
//第几页
int pageindex = ; public void getProlist()
{
string condition = hidden.Value;
rptProlist.DataSource = new PM.BLL.tb_Project().getdatabyPageIndex("tb_Project", "*", condition, pagesize, pageindex, "ProjectID", out recordCount, null, null);
rptProlist.DataBind();
AspNetPager1.RecordCount = recordCount;
AspNetPager1.PageSize = pagesize;
} /// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
string proName = txtProName.Text.Trim();
string proState = ddlState.SelectedItem.Text;
int proPrincipal = Convert.ToInt32(ddlPrincipal.SelectedValue);
int proCreater = Convert.ToInt32(ddlCreater.SelectedValue);
string proNotes = txtNotes.Text; StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty(proName))
{
sb.Append(" and ProjectName like '%" + proName + "%'");
}
if (ddlState.SelectedValue != "-1")
{
sb.Append(" and ProjectState='" + proState + "'");
}
if (proPrincipal != -)
{
sb.Append(" and ProjectPrincipal='" + proPrincipal + "'");
}
if (proCreater != -)
{
sb.Append(" and ProjectCreater='" + proCreater + "'");
}
if (!string.IsNullOrEmpty(txtStartDate.Text.Trim()) && !string.IsNullOrEmpty(txtEndDate.Text.Trim()))
{
sb.Append(" and (StartDate >= '" + Convert.ToDateTime(txtStartDate.Text).ToString("yyyy-MM-dd") + "' and StartDate <= '" + Convert.ToDateTime(txtEndDate.Text).ToString("yyyy-MM-dd") + "')");
}
if (!string.IsNullOrEmpty(proNotes))
{
sb.Append(" and ProjectNotes like '%" + proNotes + "%' ");
}
hidden.Value = Convert.ToString(sb);
string condition = Convert.ToString(sb);
pageindex = ;
getProlist();
} protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
getProlist();
} protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
pageindex = this.AspNetPager1.CurrentPageIndex - ;
getProlist();
}

把搜索条件存储在隐藏域中

ASP.NET分页存储过程,解决搜索时丢失条件信息的更多相关文章

  1. (转)asp.net分页存储过程

    Asp.Net分页存储过程 SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (sel ...

  2. Asp.Net分页存储过程

      SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (select top (当前的 ...

  3. PHP解决搜索时在URL地址栏输入中文字符搜索结果出现乱码

    这 个问题的出现的前提是本站代码采用utf-8格式,php空间当页面停留在搜索页面时,在浏览器的地址栏输入中文的关键字进行搜索时会出现乱码,在网上查找资料说 明,是因为浏览器默认将url中的中文字符编 ...

  4. 学习ASP.NET Core(08)-过滤搜索与分页排序

    上一篇我们介绍了AOP的基本概览,并使用动态代理的方式添加了服务日志:本章我们将介绍过滤与搜索.分页与排序并添加对应的功能 注:本章内容大多是基于solenovex的使用 ASP.NET Core 3 ...

  5. 1、SQL可搜索可排序可分页存储过程, 2、范围内的随机时间 适用于sql 2008以上

    -- ============================================= -- Author: 蜘蛛王 -- Create date: 2015-10-29 -- Descri ...

  6. asp.net利用存储过程分页代码

    -最通用的分页存储过程 -- 获取指定页的数据 CREATE PROCEDURE Pagination ), -- 表名 ) = '*', -- 需要返回的列 )='', -- 排序的字段名 , -- ...

  7. asp.net—执行分页存储过程的函数

    分页存储过程的T—SQL在之前的文章中已经跟大家分享过了 现在就对应 分页存储过程 跟大家分享下在.net中执行的函数. 该文章是希望给予新手一些编程过程中的帮助(大神可以帮忙指出代码中的不妥之处) ...

  8. 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

      ×   检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...

  9. asp.net调用存储过程详解

    摘要 存储过程的调用在B/S系统中用的很多.传统的调用方法不仅速度慢,而且代码会随着存储过程的增多不断膨胀,难以维护.新的方法在一定程度上解决了这些问题. 关键词 ASP.NET:存储过程   在使用 ...

随机推荐

  1. Qt浅谈之三十九圆形进度条(已经有50篇了)

    http://blog.csdn.net/taiyang1987912/article/category/2314763

  2. Leetcode解题思想总结篇:双指针

    Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...

  3. eMMC(KLM8G2FE3B)

     Tiny4412原理图中,eMMC是169-PIN,资料中对应内存为16/32G:而用户手册上eMMC内存为4G,对应的是153-PIN?    原理图中上标注:KLM8G2FE3B-B001_1. ...

  4. jdk jre jvm 关系

    很多朋友可能跟我一样,已经使用JAVA开发很久了,可是对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天特写此文,来整理下三者的关系. JDK : Java Development ...

  5. hadoop2.2编程: 重写comparactor

    要点: 类型比较在hadoop的mapreduce中非常重要,主要用来比较keys; hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比 ...

  6. bzoj1570

    购买的机票限制和数据范围很容易想到是网络流不难想到每个城市按时刻拆点,这也是一个经典模型由于时间不会太大,我们穷举时间,不断在残留网络上建图,跑最大流直至总流量为k即可 ; type node=rec ...

  7. 漫谈CSS的渲染效率

    总结了部分所学.所听.所看.所问的一些CSS写作经验,书写高效的CSS - 漫谈CSS的渲染效率,它们与渲染效率及所占用消耗的资源有一定的关 联.部分为自己理解所写,不排除会有错漏,欢迎提供更好的意见 ...

  8. UVA_11796_Dog_Distance_(计算几何)

    描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. Set up JBPM5.4 Final Installer to use MS SQL Server 2008 using JTDS(转)

    [-] A What I Am Going To Do B The Setup Steps C Lets Install it   A. What I Am Going To Do B. The Se ...

  10. EF中使用SQL函数

    SqlFunctions引用命名空间:using System.Data.Objects.SqlClient; db.Favorite.Where(_ => SqlFunctions.PatIn ...