程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET結合存儲過程寫的通用搜索分頁程序

ASP.NET結合存儲過程寫的通用搜索分頁程序

編輯:.NET實例教程

存儲過程改自bigeagle的論壇分頁程序。請大家批判!:)
select.ASPx

--------------------------------------------------------------------------------

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClIEnt" %>
<script runat="server">

    protected void Page_Load(Object sender, EventArgs e)
         {
             int intPageNo,intPageSize,intPageCount;
             intPageSize = 25;
             if (Request["CurrentPage"]==null)
                 {
                     intPageNo = 1;
                 }
             else
                 {
                     intPageNo = Int32.Parse(Request["CurrentPage"]);
                 }
            
            
             SqlConnection MySQLConnection = new SqlConnection("server=(local);Database=test;user id=sa;passWord=");
             SqlCommand mySqlCommand = new SqlCommand("up_GetTopicList", MySQLConnection);
             MySQLCommand.CommandType = CommandType.StoredProcedure;
            
             SqlParameter workParm;
            
             //搜索表字段,以","號分隔
             workParm = MySQLCommand.Parameters.Add("@a_TableList", SqlDbType.VarChar, 200);
             MySQLCommand.Parameters["@a_TableList"].Value = "OFFERID,type,offertime";
            
             //搜索表名
             workParm = MySQLCommand.Parameters.Add("@a_TableName", SqlDbType.VarChar, 30);
             MySQLCommand.Parameters["@a_TableName"].Value = "offer";
            
             //搜索條件,如"select * from aa where a=1 and b=2 and c=3"則條件為"where a=1 and b=2 and c=3"
             workParm = MySQLCommand.Parameters.Add("@a_SelectWhere", SqlDbType.VarChar, 500);
             MySQLCommand.Parameters["@a_SelectWhere"].Value = "where type='idl'";
            
             //表主鍵字段名,必須為INT類型
             workParm = MySQLCommand.Parameters.Add("@a_SelectOrderId", SqlDbType.VarChar, 50);
             MySQLCommand.Parameters["@a_SelectOrderId"].Value = "offerid";      
            
             //排序,可以使用多字段排序但主鍵字段必需在最前面
             workParm = MySQLCommand.Parameters.Add("@a_SelectOrder", SqlDbType.VarChar, 50);
             MySQLCommand.Parameters["@a_SelectOrder"].Value = "order by offerid desc";
            
             //頁號
             workParm = MySQLCommand.Parameters.Add("@a_intPageNo", SqlDbType.Int);
             MySQLCommand.Parameters["@a_intPageNo"].Value = intPageNo;
            
             //每頁顯示數
             workParm = MySQLCommand.Parameters.Add("@a_intPageSize", SqlDbType.Int);
             MySQLCommand.Parameters["@a_intPageSize"].Value = intPageSize;
            
             //總記錄數(存儲過程輸出參數)
             workParm = MySQLCommand.ParametersAdd("@RecordCount", SqlDbType.Int);
             workParm.Direction = ParameterDirection.Output;            
            
             //當前頁記錄數(存儲過程返回值)
             workParm = MySQLCommand.Parameters.Add("RowCount", SqlDbType.Int);
             workParm.Direction = ParameterDirection.ReturnValue;

             MySQLConnection.Open();
             Repeater.DataSource = MySQLCommand.ExecuteReader();                                  
            
             Repeater.DataBind();
            
             MySQLConnection.Close();
            
             Int32 RecordCount = (Int32)MySQLCommand.Parameters["@RecordCount"].Value;
             Int32 RowCount = (Int32)MySQLCommand.Parameters["RowCount"].Value;
            
             LabelRecord.Text = RecordCount.ToString();
             LabelRow.Text = intPageNo.ToString();
             intPageCount = RecordCount/intPageSize;
             if ((RecordCount%intPageSize)>0)
                 intPageCount += 1;
             LabelPage.Text = intPageCount.ToString();
            
             if (intPageNo>1)
                 {
                  &nbsp;  HLFistPage.NavigateUrl = "select.ASPx?CurrentPage=1";
                     HLPrevPage.NavigateUrl = String.Concat("select.ASPx?CurrentPage=","",intPageNo-1);
                 }
             else
                 {
                     HLFistPage.NavigateUrl = "";
                     HLPrevPage.NavigateUrl = "";
                     //HLFistPage.Enabled = false;
                     //HLPrevPage.Enabled = false;
                 }
                
             if (intPageNo<intPageCount)
                 {
                     HLNextPage.NavigateUrl = String.Concat("select.ASPx?CurrentPage=","",intPageNo+1);
                     HLEndPage.NavigateUrl = String.Concat("select.ASPx?CurrentPage=","",intPageCount);
                 }
             else
                 {
                     HLNextPage.NavigateUrl = "";
                     HLEndPage.NavigateUrl = "";
                     //HLNextPage.Enabled=false;
                     //HLEndPage.Enabled=false;
                 }
 ;           
         }

</script>
<Html>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312">
<head>
    <link href="/style.CSS" rel="stylesheet" />
<style type="text/CSS">
.high {  font-family: "宋體"; font-size: 9pt; line-height: 140%}
.mid {  font-size: 9pt; line-height: 12pt}
.small {  font-size: 9pt; line-height: normal}
.TP10_5 {
    font-size: 14px;
    line-height: 140%;
}
</style>
    <style type="text/CSS">A:link {
    COLOR: #cc6666
}
</style>
</head>
<body>
    <form runat="server">
<span class="high">              第<font color="#CC0000"><asp:Label id="LabelRow" runat="server"/></font>頁 | 共有<ASP:Label id="LabelPage" runat="server"/>頁
              | <ASP:Label id="LabelRecord" runat="server"/>條信息 |
              <ASP:HyperLink id="HLFistPage" Text="首頁" runat="server"/>
              | <ASP:HyperLink id="HLPrevPage" Text="上一頁" runat="server"/>
              | <ASP:HyperLink id="HLNextPage" Text="下一頁" runat="server"/>
              | <ASP:HyperLink id="HLEndPage" Text="尾頁" runat="server"/></span><br>
   
        <ASP:Repeater id=Repeater runat="server">

            <HeaderTemplate>

      <table width="583" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#000000"><table width="100%" border="0" cellpadding="4" cellspacing="1" class="TP10_5">
              <tr bgcolor="#999999">
                <td align="center"> <strong><font color="#FFFFFF">訂單號</font></strong></td>
                <td align="center"> <strong><font color="#FFFFFF">服務項目</font></strong></td>
         &nbsp;      <td align="center"> <strong><font color="#FFFFFF">預訂日期</font></strong></td>
                <td align="center"> <strong><font color="#FFFFFF">操作人員</font></strong></td>
                <td align="center"> <strong><font color="#FFFFFF">分配狀態</font></strong></td>
                <td> <div align="center"></div></td>
              </tr>
            </HeaderTemplate>

            <ItemTemplate>

              <tr align="center" bgcolor="#FFFFFF" class="small" onMouseOver='this.style.background="#CCCCCC"' onMouSEOut='this.style.background="#FFFFFF"'>
                <td><%# DataBinder.Eval(Container.DataItem, "offerid") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "type") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "offertime") %></td>
                <td> </td>
                <td> </td>
                <td><a href="Javascript:void(window.open('info.ASP?id=<%# DataBinder.Eval(Container.DataItem, "offerid") %>','訂單分配','height=600,width=1000'))">訂單詳情</a></td>
              </tr>

            </ItemTemplate>

            <FooterTemplate>

            </table></td>
        </tr>
      </table>

            </FooterTemplate>

        </ASP:Repeater>

    </form>
</body>
</Html>

--------------------------------------------------------------------------------


up_GetTopicList.sql

--------------------------------------------------------------------------------

CREATE proc up_GetTopicList
       @a_TableList Varchar(200),
       @a_TableName Varchar(30),
       @a_SelectWhere Varchar(500),
       @a_SelectOrderId Varchar(20),
       @a_SelectOrder Varchar(50),
       @a_intPageNo int,
       @a_intPageSize int,
       @RecordCount int OUTPUT
as
   /*定義局部變量*/
   declare @intBeginID         int
   declare @intEndID           int
   declare @intRootRecordCount int
   declare @intRowCount        int
   declare @TmpSelect          NVarchar(600)
   /*關閉計數*/
   set nocount on
  
   /*求總共根貼數*/

   select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from '+@a_TableName+' '+@a_SelectWhere
   execute sp_executesql
             @TmpSelect,
             N'@SPintRootRecordCount int OUTPUT',
             @SPintRootRecordCount=@intRootRecordCount OUTPUT

select @RecordCount = @intRootRecordCount

   if (@intRootRecordCount = 0)    --如果沒有貼子,則返回零
       return 0
      
   /*判斷頁數是否正確*/
   if (@a_intPageNo - 1) * @a_intPageSize > @intRootRecordCount
      return (-1)

   /*求開始rootID*/
   set @intRowCount = (@a_intPageNo - 1) * @a_intPageSize + 1
   /*限制條數*/

   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintBeginID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql
             @TmpSelect,
        &nbsp;    N'@SPintRowCount int,@SPintBeginID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintBeginID=@intBeginID OUTPUT


   /*結束rootID*/
   set @intRowCount = @a_intPageNo * @a_intPageSize
   /*限制條數*/

   select @TmpSelect = 'set nocount on;set rowcount @SPintRowCount;select @SPintEndID = '+@a_SelectOrderId+' from '+@a_TableName+' '+@a_SelectWhere+' '+@a_SelectOrder
   execute sp_executesql
             @TmpSelect,
             N'@SPintRowCount int,@SPintEndID int OUTPUT',
             @SPintRowCount=@intRowCount,@SPintEndID=@intEndID OUTPUT


if @a_SelectWhere='' or @a_SelectWhere IS NULL
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' where '+@a_SelectOrderId+' between '
else
   select @TmpSelect = 'set nocount off;set rowcount 0;select '+@a_TableList+' from '+@a_TableName+' '+@a_SelectWhere+' and '+@a_SelectOrderId+' between '

if @intEndID > @intBeginID
   select @TmpSelect = @TmpSelect+'@SPintBeginID and @SPintEndID'+' '+@a_SelectOrder
else
   select @TmpSelect = @TmpSelect+'@SPintEndID and @SPintBeginID'+' '+@a_SelectOrder

   execute sp_executesql
             @TmpSelect,
             N'@SPintEndID int,@SPintBeginID int',
             @SPintEndID=@intEndID,@SPintBeginID=@intBeginID

   return(@@rowcount)
   --select @@rowcount
GO

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved