编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

分页显示查询数据(分页查询思路)

wxchong 2024-07-16 10:08:24 开源技术 14 ℃ 0 评论

?查询数据后,将数据分页显示

一、前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="querypage.aspx.cs" Inherits="guolei.Web.querypage" %>
  <%@ 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 id="Head1" runat="server">   
        <title>Title</title> 
<style type="text/css">        
  *{margin:0;padding:0;} 
table.gridtable{    
  width:80%;    
  font-family: verdana,arial,sans-serif;    
  font-size:11px;    
  color:#333333;    
  border-width: 1px;    
  border-color: #666666;    
  border-collapse: collapse;
}
table.gridtable th {    
  border-width: 1px;    
  padding: 8px;   
  border-style: solid;    
  border-color: #666666;
}
table.gridtable td { 
  border-width: 1px;    
  padding: 8px;    
  border-style: solid;    
  border-color: #666666;    
  background-color: #ffffff;   
  text-align:center;
}    
</style>
</head>
<body>      
  <form id="form1" runat="server">        
    <asp:TextBox ID="txtKeywords" runat="server" CssClass="keyword" />        
    <asp:LinkButton ID="lbtnSearch" runat="server" CssClass="btn-search" onclick="btnSearch_Click">查询</asp:LinkButton>        
<asp:Button ID="btn_all" runat="server" OnClick="btn_all_Click" Text="显示所有" />        
  <asp:Button ID="btn_empty" runat="server" OnClick="btn_empty_Click" Text="清空" />    
    <div>        
    <asp:Repeater ID="RepList1" runat="server">            
      <HeaderTemplate>                
      <table class="gridtable">                    
        <thead>                        
        <tr>                        
        <th style="width: 100px;">                                顺序号                            </th>
<th style="width: 100px;">                                ID                            </th>        
<th style="width: 270px;">                                标题                            </th>        
<th style="width: 270px;">                                时间                            </th>                     
</tr>                   
</thead>           
</HeaderTemplate>  
<ItemTemplate>                
  <tbody>                    
  <tr>                        
  <td> <%# (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + Container.ItemIndex + 1%></td>                        
  <td>                            <%#Eval("id")%>                        </td>
   <td>                            <%#Eval("title")%>                        </td> 
   <td>                            <%#Eval("add_time")%>                        </td>                    
     </tr>            
</ItemTemplate>            
<FooterTemplate>                
       </tbody> 
</table>            
</FooterTemplate>        
</asp:Repeater>    
</div>    
<div class="pull-right">      
  <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%"   
NumericButtonCount="6" UrlPaging="true"       
CustomInfoHTML="总计%RecordCount%条记录,共%PageCount%页"
ShowCustomInfoSection="left"       
FirstPageText="首页" LastPageText="末页" NextPageText="下页" PrevPageText="上页"       
Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true"        
SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged" PageSize="15">   
  </webdiyer:AspNetPager>     
</div>    
</form>
</body>
</html>

二、后台代码

 protected void Page_Load(object sender, EventArgs e)        {
   string keywords = Request.QueryString["keywords"];           
   AspNetPager1.RecordCount = new DAL.title().GetCount(keywords);        
 }       
//查询按钮        
protected void btnSearch_Click(object sender, EventArgs e)        {  
  Response.Redirect("querypage.aspx?keywords=" + txtKeywords.Text);       
}       
//显示全部 
protected void btn_all_Click(object sender, EventArgs e)        {  
  Response.Redirect("querypage.aspx");        
}        
//清空
protected void btn_empty_Click(object sender, EventArgs e)        {
  txtKeywords.Text = "";            
  this.RepList1.Controls.Clear();//清空当前内容            
  RepList1.DataSource = string.Empty;            
  RepList1.DataBind();            
  AspNetPager1.RecordCount = 0;       
}       
protected void AspNetPager1_PageChanged(object src, EventArgs e)        {   
  string keywords = Request.QueryString["keywords"];           
  txtKeywords.Text = keywords;            
  RepList1.DataSource = new DAL.title().SelectbyPage(keywords,AspNetPager1.StartRecordIndex.ToString(), AspNetPager1.EndRecordIndex.ToString());            
  RepList1.DataBind();        
}

三、DAL代码

 //得到条件查询后的记录行数        
public int GetCount(string strWhere)        { 
  StringBuilder strSql = new StringBuilder();          
  strSql.Append("select count(*) from title where");           
  strSql.Append(CombSqlTxt(strWhere));           
  return Convert.ToInt32(new SqlHelper().ExecuteScalar(strSql.ToString(), CommandType.Text));       
} 
// 组合SQL查询语句==========================       
protected string CombSqlTxt(string _keywords)        { 
  StringBuilder strTemp = new StringBuilder();           
  if (!string.IsNullOrEmpty(_keywords))            { 
    strTemp.Append(" title like '%" + _keywords + "%'");           
  }
  else  
  {                
    strTemp.Append(" 1=1");           
  }            
  return strTemp.ToString();       
} 
//带有查询条件的分页 
public DataTable SelectbyPage(string strWhere, string startIndex, string endIndex) 
//当前页的首条页码参数starIndex和最后页码参数endIndex 
{    
  StringBuilder strSql = new StringBuilder();            
  strSql.Append("with temptbl as ( SELECT ROW_NUMBER() OVER (ORDER BY id desc)AS Row, * from title where ");            
  strSql.Append(CombSqlTxt(strWhere));              
  //添加条件语句            
  strSql.Append(") SELECT * FROM temptbl where Row between @startIndex and @endIndex");            
  SqlParameter[] sqlParameters =  { new SqlParameter("@startIndex", startIndex),  
    new SqlParameter("@endIndex",endIndex)};            
DataTable dt = new SqlHelper().ExecuteQuery(strSql.ToString(), sqlParameters, CommandType.Text); 
return dt;        
}

四、效果图

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表