扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:冷枫 来源:CSDN 2007年9月21日
关键字:
在本页阅读全文(共3页)
4.拖入用户控件Pager.ascx
5.copy如下cs代码
public class DataGridPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected UserControl.Pager Pager1;//定义用户控件,根据用户控件所在目录做适当的调整
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Repeater Repeater1;
public static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
BindData();
}
}

Bind Data#region Bind Data
private void BindData()
{
int output=0;
this.SortExpression="UserID asc";
this.FilterExpression="1=1";
ds = GetList(this.Pager1.CurrentPageIndex,this.Pager1.PageSize,this.SortExpression,this.FilterExpression,ref output);
if (ds!=null && ds.Tables.Count>0)
{
//DataGrid分页
DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();
//DataList分页
DataList1.DataSource=ds.Tables[0];
DataList1.DataBind();
//Repeater分页
Repeater1.DataSource=ds.Tables[0];
Repeater1.DataBind();
}
Pager1.RecordCount=output;
}
#endregion

ExecSPDataSet#region ExecSPDataSet
public static DataSet ExecSPDataSet(string sql,System.Data.IDataParameter[] paramers)
{
SqlConnection conn=new SqlConnection(ConnectionString);
SqlCommand sqlcom=new SqlCommand(sql,conn);
sqlcom.CommandType= CommandType.StoredProcedure ;
foreach(System.Data.IDataParameter paramer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=sqlcom;
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
#endregion

GetList#region GetList
public static DataSet GetList(int page_num,int row_in_page,string order_column,string comb_condition,ref int output)
{
string sql="tp_Fetch_List";
System.Data.SqlClient.SqlParameter[] p=new SqlParameter[5];
p[0]=new SqlParameter();
p[0].ParameterName ="@page_num";
p[0].Value =page_num;
p[0].DbType=System.Data.DbType.Int32;
p[1]=new SqlParameter();
p[1].ParameterName ="@row_in_page";
p[1].Value =row_in_page;
p[1].DbType=System.Data.DbType.Int32;
p[2]=new SqlParameter();
p[2].ParameterName ="@order_column";
p[2].Value =order_column;
p[2].DbType=System.Data.DbType.String;
p[3]=new SqlParameter();
p[3].ParameterName ="@row_total";
p[3].Direction=System.Data.ParameterDirection.Output;
p[3].DbType=System.Data.DbType.Int32;
p[4]=new SqlParameter();
p[4].ParameterName ="@comb_condition";
p[4].Value =comb_condition;
p[4].DbType=System.Data.DbType.String;
DataSet ds=ExecSPDataSet(sql,p);
if (p[3].Value!=DBNull.Value && p[3].Value.ToString()!=string.Empty )
output=Convert.ToInt32(p[3].Value);
return ds;
}
#endregion 

property#region property
private DataSet ds
{
get
{
return ViewState["ds"] as DataSet;
}
set
{
ViewState["ds"] = value;
}
}
public string FilterExpression
{
get
{
if (this.ViewState["FilterExpression"]!=null)
return (string)this.ViewState["FilterExpression"];
return string.Empty ;
}
set
{
this.ViewState["FilterExpression"]=value;
}
}
public string SortExpression
{
get
{
if (this.ViewState["SortExpression"]!=null)
return (string)this.ViewState["SortExpression"];
return string.Empty ;
}
set
{
this.ViewState["SortExpression"]=value;
}
}
#endregion

Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
this.Pager1.NavigationClick+=new EventHandler(Pager1_NavigationClick);
}

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Pager1_NavigationClick(object sender, EventArgs e)
{
BindData();
}
}6.测试表结构
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestGrid]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TestGrid]
GO
CREATE TABLE [dbo].[TestGrid] (
[UserID] [int] NOT NULL ,
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[State] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Enabled] [bit] NULL
) ON [PRIMARY]
GO如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。