Griffiths web design based in Royston, Hertfordshire, United Kingdom


Paging ASP.NET ListView control with a DataPager control.

Author:   Michael Griffiths

Date created:   15 December 2015

Paging ASP.NET ListView control with a DataPager control. [l]Today I would like to discuss paging a listview control. First I will show you paging a ListView that uses a SqlDataSource control then I will show you paging a ListView that does not use a DataSource control. The DataPager control makes paging almost effortless when used in conjunction with a DataSource control as you will see On your aspx page add the following markup: [code] TestCol1:
No data was returned.
[/code] We have added a SqlDataSource control, a ListView control and a DataPager control. In the ListView control we have defined an EmptyDataTemplate which, as the name suggests, is displayed if no rows were returned. We have also defined an ItemTemplate which defines the layout of each individual item, A GroupTemplate which contains a placeholder for the item and a LayoutTemplate which defines the layout of the listview and also contains our DataPager control. If you run the application you’ll see that it displays the first 5 items and when you click the little number 2 you’ll see the final 2 items. And that’s it you now have a ListView that pages through data without writing a single line of C# code. Wonderful isn’t it? If you don’t use a DataSource control there are a few modifications required for it to work. I’ll show you why. On your aspx page add the following markup: [code]
No data was returned.
[/code] As you can see the only difference between this example and the previous example is there is not a DataSource control. In your code behind add the following to populate the ListView with data. [code] private string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { getData(); } } protected void getData() { string queryString = "select * from dbo.TestTable"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(queryString, con); SqlDataAdapter adp = new SqlDataAdapter(cmd); try { con.Open(); DataSet ds = new DataSet(); adp.Fill(ds, "TestTable"); ListView1.DataSource = ds; ListView1.DataBind(); } catch (Exception er) { lblError.Text = "Error retrieving data: " + er.Message; } finally { con.Close(); } } [/code] First we added reference to the connection string that is stored in the web.config file. Then we used an ADO.NET select statement to query the database, put the data into a dataset then used the dataset as the data source of the listview. Go ahead and run the application and see what happens. It displays the first page just fine but when you click on the little number 2 you have to click it twice to see the next page but then you are unable to view the previous page again. In order to correct this you will need to implement the following steps: First add the OnPagePropertiesChanging event to the listview as follows: [code] [/code] Then in your code behind add the following code: [code] protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { DataPager dp = (DataPager)ListView1.FindControl("DataPager1"); //set current page startindex, max rows and rebind to false dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); //rebind List View getData(); } [/code] What this code does is first it finds the DataPager control. Then it sets the current page start index, max rows and rebind to false. Then it re-binds the ListView. If you run the application you’ll see that now it pages like a dream. I have included a downloadable project for you to examine and play with. I hope you found this tutorial helpful and easy to follow if you have any questions or comments please add them below in the comments section or if you prefer head over to the contact page and fill in the form to send a message directly to my email. If you know of anyone who may find this article interesting or helpful then please share it around.[/l]

About the author

My name is Michael Griffiths, I am a website designer and developer and the owner of Griffiths Web Design. I am based in Royston, Hertfordshire. Most of my work is local, Hertfordshire, Cambridgeshire, Bedfordshire, Essex etc. but that’s not to say I won’t work elsewhere. One of the wonders of modern technology is remote working. So no matter where the client is located it is always possible to connect.

I specialise in creating ASP.NET webforms applications using C#. I use Microsoft SQL Server for database development and on the front end I use Html, CSS and JavaScript. I am also proficient in the use of jQuery and several CSS frameworks.

One of my greatest passions in life is learning. Which is why I chose to pursue a career in web design and development, technology is in a state of constant and rapid evolution which means there is always something new to learn. I also enjoy writing, both fiction and non-fiction.


Please feel free to leave a comment below:

finance advice

Hi! I know this is kinda off topic however I'd figured I'd ask. Would you be interested in exchanging links or maybe guest writing a blog article or vice-versa? My site addresses a lot of the same topics as yours and I believe we could greatly benefit from each other. If you are interested feel free to send me an e-mail. I look forward to hearing from you! Terrific blog by the way! finance advice

nikon camera

After exploring a handful of the blog posts on your site, I truly appreciate your technique of blogging. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Take a look at my website as well and tell me your opinion. nikon camera

small business adviser

My partner and I stumbled over here different website and thought I should check things out. I like what I see so now i'm following you. Look forward to exploring your web page repeatedly. small business adviser

education advice

Please let me know if you're looking for a article writer for your weblog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd really like to write some content for your blog in exchange for a link back to mine. Please send me an email if interested. Kudos! education advice

national health advice

I think this is among the most significant information for me. And i am glad reading your article. But wanna remark on some general things, The site style is perfect, the articles is really nice : D. Good job, cheers national health advice

electronic outlet

Good post. I learn something totally new and challenging on sites I stumbleupon every day. It will always be exciting to read content from other writers and practice something from their web sites. electronic outlet

value investing

I don't even know how I ended up here, but I thought this post was great. I don't know who you are but certainly you're going to a famous blogger if you aren't already ;) Cheers! value investing

best buy outlet

Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your website? My blog is in the exact same area of interest as yours and my visitors would genuinely benefit from some of the information you present here. Please let me know if this ok with you. Thanks!| best buy outlet

business owner

Appreciate this post. Will try it out. business owner

gold tip

Hello it's me, I am also visiting this web page on a regular basis, this site is actually good and the viewers are really sharing good thoughts. gold tip

starting a business

It's really a nice and helpful piece of info. I am happy that you simply shared this helpful info with us. Please stay us up to date like this. Thanks for sharing. starting a business

investment broker

I absolutely love your blog and find almost all of your post's to be exactly I'm looking for. can you offer guest writers to write content for yourself? I wouldn't mind publishing a post or elaborating on many of the subjects you write with regards to here. Again, awesome web site! investment broker

home & family recipes

I am not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission. home & family recipes


I think everything said was actually very logical. However, consider this, what if you typed a catchier post title? I am not suggesting your information is not good., but suppose you added something to maybe get people's attention? I mean %BLOG_TITLE% is kinda plain. You could peek at Yahoo's front page and see how they create article headlines to grab people interested. You might add a related video or a related picture or two to grab people interested about everything've got to say. In my opinion, it would make your blog a little livelier.| Klefer

Griffiths Web Design Logo by Michael Griffiths
Griffiths Web Design Logo by Michael Griffiths
Click to go to the top of the page