|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Figure 1 - ASP.NET Pager Control
IntroductionPaging is an important thing that every web developer should know about. In ASP.NET only Note 1: Although provided source and sample projects are categorized by the ASP.NET version, the Pager control's source is the same inside both ASP.NET 2.0 and ASP.NET 3.5 projects, but the ASP.NET 3.5 project has a sample to show how to utilize LINQ as Pager control's data provider. What a Paging System Needs and What it is Supposed to ProvideEssential things we should provide to our paging system:
Things the paging system is supposed to provide:
What Parts does it Consist Of?This paging system consists of the following parts and I will try to explain them: The first part:
A data access engine which gets required parameters from the Pager control and query database or other data source. Again, if you are using ASP.NET 3.5 maybe you want to write this part with LINQ otherwise this part is a Stored Procedure. Second Part:
The Pager control which generates the hyperlinks for us in a user friendly way. These hyperlinks will be used by user to navigate through the pages. The last but not the least:
And finally, a web page which hosts the Pager control and show paged results. Deploying and customizing Pager controlStep by step explanation of Pager control's deployment 1. Write your Own Business Specific Stored Procedure or LINQ Query1.1 Paging on SQL Server 2000Let me start with Stored Procedure. It comes with the demo project that you may have downloaded. Take a look at the screenshot below:
(You may want to change the Procedure name, but don't forget to change the procedure name in the host page too) As you know, a stored procedure is a business specific object, and its parameter names are strongly dependent on the names of your business objects. So, you should customize some of its variables. To make the world easy for myself and of course for you, I colorized the sections we have a special focus on them. To modify the stored procedure to fit into your business, do the following. We will start from the bottom to top:
1.2 Paging on SQL Server 2005SQL Server 2005 supports paging internally via the
Create the stored procedure in your database, and carry on. 2. Declare and Customize Pager ControlLet's see how we can use it and how it can play its role in our paging system. You can customize the Pager control via its properties. I've divided the properties into two main categories: Globalization As the name shows, the language of the captions in the Pager control can be changed with these properties. Let's take a look at the screenshot below:
Default language is (en-us) but you can easily change this property to change the Pager control's caption language, even to Unicode languages like Persian or Arabic. Also, there is a property, named RTL, which changes the direction of the Pager control from Behavioural You can change the behaviour of your Pager control here. Actually, the main customization happens here.
Basic Behavioural Properties
SmartShortcuts PropertiesSmartShortcuts were introduced in V2.0 and they improved efficiency, especially in large scale data scenarios. They are really cool and are shown in figure-1 in gray backgrounded cells.
Hidden Hyperlinks PropertiesAfter releasing the PagerControl V2.0 some developers contacted me and requested the previous version. I asked them why they wanted the previous version and I found out they were interested in the first version because it was based on
To see the hyperlinks in action switch this property on and view the page source 3. The Host Web PageLet's continue and complete our paging system. As you can see in the demo project, we have a web page which hosts our controls (
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRepeater();
}
}
If a user clicks on a hyperlink to naviaget to a page, the public void pager_Command(object sender, CommandEventArgs e)
{
int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
pager1.CurrentIndex = currnetPageIndx;
BindRepeater();
}
To get the paged results private void BindRepeater()
{
string strConn = ConfigurationManager.ConnectionStrings[
"northwindConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(strConn);
SqlCommand Cmd = new SqlCommand("dbo.GetPagedProducts_sql2k5", cn);
Cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr;
Cmd.Parameters.Add("@PageSize", SqlDbType.Int, 4).Value = pager1.PageSize;
Cmd.Parameters.Add("@CurrentPage", SqlDbType.Int, 4).Value = pager1.CurrentIndex;
Cmd.Parameters.Add("@ItemCount", SqlDbType.Int).Direction = ParameterDirection.Output;
cn.Open();
dr = Cmd.ExecuteReader();
rptProducts.DataSource = dr;
rptProducts.DataBind();
dr.Close();
cn.Close();
Int32 _totalRecords = Convert.ToInt32(Cmd.Parameters["@ItemCount"].Value);
pager1.ItemCount = _totalRecords;
}
Colorize and Customizing the Pager Control's StyleHope everything is going well. If you are done with deployment, let's go to customize the Pager control's style. I've created two CSS stylesheets that can colorize the Pager control in two different ways. LightStyle.css: Provides the below style for your Pager control (recommended for light background web pages).
DarkStyle.css: Provides the below style for your Pager control (recommended for dark background web pages).
(To customize the control's style in your preferred way, you should manipulate the stylesheet classes in the "Styles" folder). AcknowledgementsLaunching V2.8 coincided with the 3rd anniversary of the ASP.NET Pager Control. I just wanted to say thanks to every single feedback you gave. I think without your help this control couldn't come this far. If you are using this control and you are happy with it, thats great. And I am happy to hear from you if you have a special feature in mind; features are important to me because they can help this control grow and become more useful. History
|
||||||||||||||||||||||