|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article will show you how to implement paging with the ASP.NET Get started!Open Visual Studio and open a new J# ASP.NET Web Application. Add a LimitationsImportant: You can only use paging for a data source that implements the The data sourceIn this example, we will show data from a MS SQL database. If the page request is not a postback, do a database lookup. private void Page_Load(Object sender, System.EventArgs e)
{
if ( !this.get_IsPostBack() )
{
// Initialize the DataGrid
this.InitializeDataGrid();
}
}
private void InitializeDataGrid( )
{
try
{
// DB connection string
String _connectionString =
"Data Source=localhost\\MinDB;Initial " +
"Catalog=BMWCCN;User Id=aspnet;Password=aspnet";
// Open a connection to the database using SQL login
System.Data.SqlClient.SqlConnection _connection =
new System.Data.SqlClient.SqlConnection( _connectionString );
// The sql statement
String _sql = "SELECT * FROM Forum ORDER BY MeldingsId";
// Creates a DataTable for the sql result.
System.Data.DataTable _dataTable = new System.Data.DataTable( );
// Uses a DataAdapter to get the data from the database.
System.Data.SqlClient.SqlDataAdapter _adapter =
new System.Data.SqlClient.SqlDataAdapter( _sql, _connection );
_adapter.Fill( _dataTable );
// Sets the datasource and binds it to the DataGrid.
this.DataGrid1.set_DataSource( _dataTable );
this.DataGrid1.DataBind();
}
catch ( Exception ex )
{
this.get_Trace().Warn( ex.getMessage() );
}
}
The PageIndexHanderAdd a “Page index changed” event handler to the control. Open the design view. Right click it and choose “Properties”. Click on the lightning icon in the Properties window to show all event handlers. Double click the “ private void DataGrid1_PageIndexChanged (Object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.set_CurrentPageIndex( e.get_NewPageIndex() );
this.InitializeDataGrid( );
}
This should all there is to be there for simple paging. You can go forward or backwards with the “<” and the “>”. If you want numbers instead of the “<” and the “>” to navigate in the pages, please do: switch to design mode in Visual Studio, right click the control and choose “Properties”, under “Styles” locate the “ ConclusionWe have seen in this short article how we can easily make paging support for our
|
||||||||||||||||||||||