自定义分页的方法技巧
privateOleDbConnectioncon=newOleDbConnection();
privatevoidPage_Load(objectsender,System.EventArgse)
{
OpenDataBase();
BindDataGrid();
}
privatevoidOpenDataBase()
{
con.ConnectionString="Provider=SqlOleDb;WorkStationID=127.0.0.1;InitialCatalog=Northwind;UserID=sa;PassWord=;ConnectTimeOut=30;PacketSize=1024;PersistSecurityInfo=false";
con.Open();
}
publicvoidBindDataGrid()
{
OleDbConnectionmyConnection=con;
DataSetds=newDataSet();
OleDbDataAdapteradapter=newOleDbDataAdapter("SelectLastName,FirstName,AddressFromEmployees",myConnection);
adapter.Fill(ds,"Document");
dgdNorthwind.DataSource=ds.Tables["Document"].DefaultView;
dgdNorthwind.DataBind();
ShowState();
}
privatevoidShowState()
{
lblPageCount.Text="总共"+dgdNorthwind.PageCount+"页";
lblCurrentPageIndex.Text="第"+(dgdNorthwind.CurrentPageIndex+1).ToString()+"页";
}
privatevoiddgdNorthwind_PageIndexChanged(objectsource,System.Web.UI.WebControls.DataGridPageChangedEventArgse)
{
dgdNorthwind.CurrentPageIndex=e.NewPageIndex;
BindDataGrid();
}
privatevoidClickButton(objectsender,EventArgse)
{
stringarg=((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case"Next":
if(dgdNorthwind.CurrentPageIndex<(dgdNorthwind.PageCount-1))
{
dgdNorthwind.CurrentPageIndex+=1;
}
break;
case"Prev":
if(dgdNorthwind.CurrentPageIndex>0)
{
dgdNorthwind.CurrentPageIndex-=1;
}
break;
case"Last":
dgdNorthwind.CurrentPageIndex=dgdNorthwind.PageCount-1;
break;
default:
dgdNorthwind.CurrentPageIndex=0;
break;
}
BindDataGrid();
}
privatevoidPage_Load(objectsender,System.EventArgse)
{
OpenDataBase();
BindDataGrid();
}
privatevoidOpenDataBase()
{
con.ConnectionString="Provider=SqlOleDb;WorkStationID=127.0.0.1;InitialCatalog=Northwind;UserID=sa;PassWord=;ConnectTimeOut=30;PacketSize=1024;PersistSecurityInfo=false";
con.Open();
}
publicvoidBindDataGrid()
{
OleDbConnectionmyConnection=con;
DataSetds=newDataSet();
OleDbDataAdapteradapter=newOleDbDataAdapter("SelectLastName,FirstName,AddressFromEmployees",myConnection);
adapter.Fill(ds,"Document");
dgdNorthwind.DataSource=ds.Tables["Document"].DefaultView;
dgdNorthwind.DataBind();
ShowState();
}
privatevoidShowState()
{
lblPageCount.Text="总共"+dgdNorthwind.PageCount+"页";
lblCurrentPageIndex.Text="第"+(dgdNorthwind.CurrentPageIndex+1).ToString()+"页";
}
privatevoiddgdNorthwind_PageIndexChanged(objectsource,System.Web.UI.WebControls.DataGridPageChangedEventArgse)
{
dgdNorthwind.CurrentPageIndex=e.NewPageIndex;
BindDataGrid();
}
privatevoidClickButton(objectsender,EventArgse)
{
stringarg=((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case"Next":
if(dgdNorthwind.CurrentPageIndex<(dgdNorthwind.PageCount-1))
{
dgdNorthwind.CurrentPageIndex+=1;
}
break;
case"Prev":
if(dgdNorthwind.CurrentPageIndex>0)
{
dgdNorthwind.CurrentPageIndex-=1;
}
break;
case"Last":
dgdNorthwind.CurrentPageIndex=dgdNorthwind.PageCount-1;
break;
default:
dgdNorthwind.CurrentPageIndex=0;
break;
}
BindDataGrid();
}
本文地址:http://www.45fan.com/dnjc/69667.html