![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190248239.JPG)
1.建立用户控件Pager.ascx
1.1 html
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249113.gif)
<script language="javascript">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
function callButtonEvent()
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
var keycode =window.event.keyCode;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if(keycode==13)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if(check()==true)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
event.cancelBubble=true;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
event.returnValue=false;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
document.getElementById('<%=btnGo.ClientID%>').click();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
function check()
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
var count = parseInt(document.getElementById('<%=lblTotal.ClientID%>').outerText);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
var txt = document.getElementById('<%=txtCurrentPage.ClientID%>').value;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
var cur = parseInt(txt);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if ((cur | NaN) ==0)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
alert('Input page must format as integer.');
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
event.cancelPostBack=true;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return false;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (cur > count || cur < 1)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
alert('Input page no out of range.');
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
event.cancelPostBack=true;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return false;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250720.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</script>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TABLE ID="Table1" CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BORDER="0">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<colgroup>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="400">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="50">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="50">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="40">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="20">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="40">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="40">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="50">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<col width="70">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</colgroup>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TR align="right">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td></td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><asp:LinkButton id="btnFirstPage" runat="server" CommandArgument="First">第一页</asp:LinkButton></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><asp:LinkButton id="btnPrevPage" runat="server" CommandArgument="Prev">上一页</asp:LinkButton></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><ASP:TEXTBOX ID="txtCurrentPage" RUNAT="server" MAXLENGTH="3" Width="40">0</ASP:TEXTBOX></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><ASP:LABEL ID="labOf" RUNAT="server">of</ASP:LABEL></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><ASP:LABEL ID="lblTotal" RUNAT="server">0</ASP:LABEL></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><ASP:BUTTON ID="btnGo" RUNAT="server" TEXT="转到" COMMANDARGUMENT="Go" ToolTip="转到"></ASP:BUTTON></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><asp:LinkButton id="btnNextPage" runat="server" CommandArgument="Next">下一页</asp:LinkButton></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<TD><asp:LinkButton id="btnLastPage" runat="server" CommandArgument="Last">最后一页</asp:LinkButton></TD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</TR>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</TABLE>
1.2 cs代码
2.建立DataGridPage.aspx
3.copy如下html代码
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<HTML>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<HEAD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<title>DataGridPage</title>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<meta content="C#" name="CODE_LANGUAGE">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<meta content="JavaScript" name="vs_defaultClientScript">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</HEAD>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<body MS_POSITIONING="GridLayout">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<form id="Form1" method="post" runat="server">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 632px; POSITION: absolute; TOP: 40px"
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
runat="server"></asp:datagrid><uc1:pager id="Pager1" runat="server"></uc1:pager><asp:datalist id="DataList1" style="Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 40px"
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
runat="server">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<ItemTemplate>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<table>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td>用户ID:</td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249113.gif)
<td><%
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
# DataBinder.Eval(Container.DataItem, "UserID") %></td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td>用户名:</td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249113.gif)
<td><%
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
# DataBinder.Eval(Container.DataItem, "UserName") %></td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</table>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</ItemTemplate>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</asp:datalist>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<asp:repeater id="Repeater1" runat="server">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<HEADERTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<table cellpadding="0" cellspacing="0" border="0">
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td>用户ID</td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td>用户名:</td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</HEADERTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<ITEMTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249113.gif)
<%
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
# DataBinder.Eval(Container.DataItem, "UserID")%>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249113.gif)
<td><%
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
# DataBinder.Eval(Container.DataItem, "UserName") %></td>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</tr>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</ITEMTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
<FOOTERTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</table>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</FOOTERTEMPLATE>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</asp:repeater></form>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</body>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
</HTML>
4.拖入用户控件Pager.ascx
5.copy如下cs代码
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
public class DataGridPage : System.Web.UI.Page
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249677.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
protected System.Web.UI.WebControls.DataGrid DataGrid1;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
protected UserControl.Pager Pager1;//定义用户控件,根据用户控件所在目录做适当的调整
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
protected System.Web.UI.WebControls.DataList DataList1;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
protected System.Web.UI.WebControls.Repeater Repeater1;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
private void Page_Load(object sender, System.EventArgs e)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (!this.IsPostBack)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
BindData();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
Bind Data#region Bind Data
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
private void BindData()
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
int output=0;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.SortExpression="UserID asc";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.FilterExpression="1=1";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
ds = GetList(this.Pager1.CurrentPageIndex,this.Pager1.PageSize,this.SortExpression,this.FilterExpression,ref output);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (ds!=null && ds.Tables.Count>0)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
//DataGrid分页
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataGrid1.DataSource = ds.Tables[0];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataGrid1.DataBind();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
//DataList分页
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataList1.DataSource=ds.Tables[0];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataList1.DataBind();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
//Repeater分页
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
Repeater1.DataSource=ds.Tables[0];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
Repeater1.DataBind();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
Pager1.RecordCount=output;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
#endregion
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
ExecSPDataSet#region ExecSPDataSet
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
public static DataSet ExecSPDataSet(string sql,System.Data.IDataParameter[] paramers)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
SqlConnection conn=new SqlConnection(ConnectionString);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
SqlCommand sqlcom=new SqlCommand(sql,conn);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
sqlcom.CommandType= CommandType.StoredProcedure ;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
foreach(System.Data.IDataParameter paramer in paramers)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
sqlcom.Parameters.Add(paramer);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
conn.Open();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
SqlDataAdapter da=new SqlDataAdapter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
da.SelectCommand=sqlcom;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataSet ds=new DataSet();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
da.Fill(ds);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
conn.Close();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return ds;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
#endregion
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
GetList#region GetList
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
public static DataSet GetList(int page_num,int row_in_page,string order_column,string comb_condition,ref int output)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
string sql="tp_Fetch_List";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
System.Data.SqlClient.SqlParameter[] p=new SqlParameter[5];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[0]=new SqlParameter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[0].ParameterName ="@page_num";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[0].Value =page_num;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[0].DbType=System.Data.DbType.Int32;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[1]=new SqlParameter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[1].ParameterName ="@row_in_page";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[1].Value =row_in_page;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[1].DbType=System.Data.DbType.Int32;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[2]=new SqlParameter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[2].ParameterName ="@order_column";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[2].Value =order_column;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[2].DbType=System.Data.DbType.String;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[3]=new SqlParameter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[3].ParameterName ="@row_total";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[3].Direction=System.Data.ParameterDirection.Output;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[3].DbType=System.Data.DbType.Int32;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[4]=new SqlParameter();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[4].ParameterName ="@comb_condition";
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[4].Value =comb_condition;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
p[4].DbType=System.Data.DbType.String;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
DataSet ds=ExecSPDataSet(sql,p);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (p[3].Value!=DBNull.Value && p[3].Value.ToString()!=string.Empty )
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
output=Convert.ToInt32(p[3].Value);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return ds;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
#endregion
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
property#region property
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
private DataSet ds
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
get
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return ViewState["ds"] as DataSet;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
set
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
ViewState["ds"] = value;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
public string FilterExpression
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
get
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (this.ViewState["FilterExpression"]!=null)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return (string)this.ViewState["FilterExpression"];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return string.Empty ;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
set
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.ViewState["FilterExpression"]=value;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
public string SortExpression
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
get
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
if (this.ViewState["SortExpression"]!=null)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return (string)this.ViewState["SortExpression"];
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
return string.Empty ;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
set
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.ViewState["SortExpression"]=value;
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
#endregion
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
Web Form Designer generated code#region Web Form Designer generated code
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
override protected void OnInit(EventArgs e)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
//
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
//
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
InitializeComponent();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
base.OnInit(e);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.Pager1.NavigationClick+=new EventHandler(Pager1_NavigationClick);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250614.gif)
/**//// <summary>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
/// Required method for Designer support - do not modify
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
/// the contents of this method with the code editor.
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
/// </summary>
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
private void InitializeComponent()
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
this.Load += new System.EventHandler(this.Page_Load);
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
#endregion
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
private void Pager1_NavigationClick(object sender, EventArgs e)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250572.gif)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190249581.gif)
{
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250619.gif)
BindData();
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250906.gif)
}
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250720.gif)
}
6.测试表结构
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestGrid]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
drop table [dbo].[TestGrid]
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
GO
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
CREATE TABLE [dbo].[TestGrid] (
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
[UserID] [int] NOT NULL ,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
[Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
[State] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
[Enabled] [bit] NULL
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
) ON [PRIMARY]
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
GO
7.测试存储过程
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
create PROCEDURE tp_Fetch_List(
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@page_num INT,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@row_in_page INT,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@order_column VARCHAR(50),
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@row_total INT OUTPUT,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@comb_condition VARCHAR(500)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
AS
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET NOCOUNT ON
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
DECLARE
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@jcc_status INT,
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@sql NVARCHAR(4000),
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
@row_ahead INT
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @jcc_status = 0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @row_ahead = (@page_num-1) * @row_in_page
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql='SELECT TOP '+ cast(@row_in_page as varchar(255)) + ' * FROM ( '
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + 'SELECT *
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
FROM TestGrid
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
) as A where 1=1'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF LEN(@comb_condition)>0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' AND (' + @comb_condition + ')'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + 'and UserID not in ( select UserID from ('
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + 'SELECT TOP ' + cast(@row_ahead as varchar(255)) + ' * From ('
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + 'SELECT *
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
FROM TestGrid
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
) as A where 1=1'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF LEN(@comb_condition)>0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' AND ( ' + @comb_condition + ' )'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF LEN(@order_column)>0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' ORDER BY ' + @order_column + ' ) AS B )'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
ELSE
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' ) AS B )'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF LEN(@order_column)>0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' ORDER BY ' + @order_column
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
print @sql
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
EXEC (@sql)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql= N'SELECT @row_total=COUNT(*) FROM ('
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + 'SELECT *
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
FROM TestGrid
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
) as A where 1=1'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF LEN(@comb_condition)>0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SET @sql = @sql + ' AND (' + @comb_condition + ')'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
print @sql
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
EXEC sp_executesql @sql,N'@row_total INT OUT',@row_total OUT
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF @@ERROR != 0
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
SELECT @jcc_status = -98
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
exit_bk:
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
-- exit with MS SQL Server error
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
IF @jcc_status = -98
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
BEGIN
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
RAISERROR ('MS SQL Server error, please contact your system administrator.',16,1)WITH NOWAIT
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
RETURN (@jcc_status)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
-- normal exit
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
RETURN (0)
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
END
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
GO
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
-- declare @aa int
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
-- exec tp_Fetch_List 1,10,'',@aa out ,'1=1'
![](http://w2.starone.cn/Article/UploadFiles/200601/20060125190250404.gif)
-- select @aa
8.源代码下载源代码下载