Webdiyer的分页控件+通用存储过程+查询+ajax分页

default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  
<asp:ScriptManager id="ScriptManager1" runat="server">
    
</asp:ScriptManager>
     
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        
<ContentTemplate>
    
<asp:Repeater ID="Repeater1" runat="server">
            
<HeaderTemplate>
               
              
        
<table width="100%" border="1" cellspacing="0" cellpadding="4" style="border-collapse:collapse">
        
<tr style="backGround-color:#CCCCFF"><th style="width:15%"><asp:Literal ID="Literal1" runat="server" Text="ProductID" /></th><th style="width:15%"><asp:Literal ID="Literal3" runat="server" Text="SupplierID" /></th><th style="width:30%"><asp:Literal ID="Literal5" runat="server" Text="CategoryID" /></th><th style="width:20%"><asp:Literal ID="Literal2" runat="server" Text="QuantityPerUnit" /></th></tr>
        
</HeaderTemplate>
        
<ItemTemplate>
        
<tr style="background-color:#FAF3DC">
        
<td><%#GetArticleTitleSummary(Convert.ToString(DataBinder.Eval(Container.DataItem,"productname")))%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "id")%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "productprice")%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "productaddtime")%></td>
        
</tr>
        
</ItemTemplate>
        
<AlternatingItemTemplate>
        
<tr style="background-color:#eaeaea">
        
<td><%#GetArticleTitleSummary(Convert.ToString(DataBinder.Eval(Container.DataItem,"productname")))%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "id")%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "productprice")%></td>
        
<td><%#DataBinder.Eval(Container.DataItem, "productaddtime")%></td>
        
</tr>
        
</AlternatingItemTemplate>
        
<FooterTemplate>
        
</table>
        
</FooterTemplate>
        
</asp:Repeater>
    
<asp:TextBox id="textbox1" runat="server"></asp:TextBox>
 
<asp:Button id="button1" runat="server"  OnClick="btn_search_Click"/>
 
 
<webdiyer:aspnetpager id="AspNetPager1" runat="server" HorizontalAlign="Center" Width="100%" pagesize="10"  OnPageChanged="AspNetPager1_PageChanged" AlwaysShow="True"></webdiyer:aspnetpager>
 
<asp:Label id="lable1" runat="server"></asp:Label>
       
</ContentTemplate>
          
</asp:UpdatePanel>         
</asp:Content>

default.aspx.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;

public partial class _Default : System.Web.UI.Page
{
    
const string vsKey = "searchCriteria";
    
const string vsKey1 = "searchCriteria1";
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
  
{
  ShowData(
string.Empty,string.Empty);
  }

    }

    
protected void btn_search_Click(object sender, EventArgs e)
    
{
        
string s = "productname like '%"+textbox1.Text+"%'";
        ViewState[vsKey] 
= s;
        
string s1 = "where productname like '%" + textbox1.Text + "%' ";
        ViewState[vsKey1] 
= s1;
         ShowData(s,s1);
        
    }

    
private void ShowData(string str1,string str2)
    
{
        AspNetPager1.RecordCount 
= Class1.dddd(str2);
       
// lable1.Text = "SELECT COUNT(*) FROM productlistnew " + str2 + "";
        Repeater1.DataSource = Class1.ddd(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize,str1);
        Repeater1.DataBind();
    }


    
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    
{
        ShowData((
string)ViewState[vsKey], (string)ViewState[vsKey1]);
       
    }

    
protected string GetArticleTitleSummary(string strTitle)
    
{
        
if (strTitle.Length <= 7)
        
{
            
return strTitle;
        }

        
return (strTitle.Substring(07)  );
    }


}



public class Class1
{
    public static string cs = ConfigurationSettings.AppSettings["SqlServerConnectionString"];
    public static AdoHelper helper = AdoHelper.CreateHelper("sqlDA");

 public static DataSet ddd(int str1, int str2,string str3)// dataset 换成 IDataReader
    {
        IDataParameter[] para = new IDataParameter[7];
        para = helper.GetSpParameterSet(cs, "SP_Pagination");
        para[0].Value = "productlistnew";//表名称,视图
        para[1].Value = "id";//主关键字
        para[2].Value = "id Desc";//排序语句,不带Order By 比如:ArticleID Desc
        para[3].Value = str1;//当前页码
        para[4].Value = str2;//分页尺寸
        para[5].Value = "*";//;要查询的字段
        para[6].Value = str3;// Filter :过滤语句,不带Where
        para[7].Value = "";//Group语句,不带Group By
        return helper.ExecuteDataset(cs, CommandType.StoredProcedure, "SP_Pagination", para);
    }

    public static int dddd(string str4)
    {
       
        return Convert.ToInt32(helper.ExecuteScalar(cs, System.Data.CommandType.Text, "SELECT COUNT(*) FROM productlistnew " + str4 +""));
   
    }
  

 

存储过程


CREATE PROCEDURE SP_Pagination
/*
***************************************************************
**     通用分页存储过程                      **
***************************************************************
参数说明:
1.Tables             :表名称,视图
2.PrimaryKey         :主关键字
3.Sort               :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
4.CurrentPage        :当前页码
5.PageSize           :分页尺寸
6.Filter             :过滤语句,不带Where
7.Group              :Group语句,不带Group By
***************************************************************/
(
@Tables varchar(1000),
@PrimaryKey varchar(100),
@Sort varchar(200) = NULL,
@CurrentPage int = 1,
@PageSize int = 10,
@Fields varchar(1000) = '*',
@Filter varchar(1000) = NULL,
@Group varchar(1000) = NULL
)
AS

/*默认排序*/
IF @Sort IS NULL OR @Sort = ''
 SET @Sort = @PrimaryKey

DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int

/*设定排序语句.*/
IF CHARINDEX('DESC',@Sort)>0
 BEGIN
  SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
  SET @operator = '<='
 END
ELSE
 BEGIN
  IF CHARINDEX('ASC', @Sort) > 0
   SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
  ELSE
   SET @strSortColumn = @Sort
  SET @operator = '>='
 END


IF CHARINDEX('.', @strSortColumn) > 0
 BEGIN
  SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
  SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
 END
ELSE
 BEGIN
  SET @SortTable = @Tables
  SET @SortName = @strSortColumn
 END

SELECT @type=t.name, @prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name = @SortTable AND c.name = @SortName

IF CHARINDEX('char', @type) > 0
   SET @type = @type + '(' + CAST(@prec AS varchar) + ')'

DECLARE @strPageSize varchar(50)
DECLARE @strStartRow varchar(50)
DECLARE @strFilter varchar(1000)
DECLARE @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)

/*默认当前页*/
IF @CurrentPage < 1
 SET @CurrentPage = 1

/*设置分页参数.*/
SET @strPageSize = CAST(@PageSize AS varchar(50))
SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50))

/*筛选以及分组语句.*/
IF @Filter IS NOT NULL AND @Filter != ''
 BEGIN
  SET @strFilter = ' WHERE ' + @Filter + ' '
  SET @strSimpleFilter = ' AND ' + @Filter + ' '
 END
ELSE
 BEGIN
  SET @strSimpleFilter = ''
  SET @strFilter = ''
 END
IF @Group IS NOT NULL AND @Group != ''
 SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
 SET @strGroup = ''
 
/*执行查询语句*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
'
)

GO

posted on 2008-09-06 22:51  草原和大树  阅读(1148)  评论(2编辑  收藏  举报