Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

I don’t know why it never occurred to me to try this, but I wanted to write this quick post regarding this little tidbit of information because others might have been wondering the same thing as me.

I described how I use paging using the ObjectDataSource and LINQ on this post:

So what if you want to page some data that also includes extra parameters? For example, lets say I have a web app that allows users to create several blogs that each have their own entries. I want to be able to page those blog entries, and I want to select them for only a certain blog Id (from a drop down or a query string, or session value, etc.)

It’s pretty straight forward as to how to do that:

Step 1: Write Two methods in your ClassName.cs file (in the example, Blog.cs)

This is again very similar to my example linked above. I would write a method that I call SelectPagedRecords (you can call it whatever you’d like) and I would have the first two parameters be startRowIndex and maximumRows. I’d add in a third parameter called blogId, which is the value I pass in using my ObjectDataSource. Here’s the method signature for this one:

public IEnumerable<BlogEntry> SelectPagedRecords(int startRowIndex, int maximumRows, int blogId)


Or an image if it helps you see it better:


It’s underlined red since I don’t return anything yet. That’s the next step.

I would open up a dataContext (somethingDataContext db = new somethingDataContext();) and then return all the blog entries where their blogID_FK is equal to the blogId in the parameter, and skip the startrowindex, and take the maximumRows. It’d look like this:

return db.BlogEntries
                .Where(m => m.BlogID_FK == blogId)


The other method to write is the GetRecordCount method. We can’t just use a GetRecordCount() because that would return the count of all blog entries, which we don’t want. We want to return the count of only the blog entries for a certain BlogId. You guessed it, we’d write in a parameter. It would look like this:


Inside, the method would return the count for all the entries of a certain BlogId:

return db.BlogEntries
               .Where(m => m.BlogID_FK == blogId)


Step 2: Creating the ObjectDataSource

Not much changes here. We’d drop in our ListView (or whatever databinding control you want to use), and configure it. It will prompt for a data source, and we can create a new one, and select ObjectDataSource.

You’ll select BlogEntries as your data type, and select “SelectPagedRecords”. On the page where it asks you to select parameters, leave the startRowIndex and maximumRows blank.

For the BlogId parameter, select something that you’ll use to separate out the blog entries. I’m using a drop down in my example, so I select that as a control parameter.

Here are the key steps broken down:

Choose the proper method:


Leave the parameters blank except for the one you want to set. In this example, I’m setting BlogID to come from a dropdown I previously added to the page.


You don’t have to set a default value, but I tend to, to avoid any possible string input errors.

Final Step: Clean up the ObjectDataSource in the code


Switch to Source view, and you’ll notice the ObjectDataSource has this in the selectParameters:

      <asp:Parameter Name="startRowIndex" Type="Int32" />
      <asp:Parameter Name="maximumRows" Type="Int32" />
      <asp:ControlParameter ControlID="BlogDropDownList" Name="BlogId" PropertyName="SelectedValue"
             Type="Int32" DefaultValue="0" />



Select the first two Parameters (startRowIndex and maximumRows) and delete them. It’ll end up looking like this:

             <asp:ControlParameter ControlID="BlogDropDownList" Name="BlogId" PropertyName="SelectedValue"
                 Type="Int32" DefaultValue="0" />

Add in to EnablePaging=”True” and set the SelectCountMethod=”GetRecordCount”. (Or whatever you named it.) Here’s how that’ll look:


Assuming you set up your ListView (or whatever databound control) to enable paging, you will now be able to page through your data records as well as pass in any number of extra parameters.




Posted on Thursday, October 29, 2009 5:52 PM | Back to top

Comments on this post: Quick Tip: Custom Paging with Extra Parameters in the Method

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © samerpaul | Powered by: