Limit rows in table per page - asp.net

I would like to be able to limit the number of rows that can be in table on View page (I am using ASP.NET MVC), and if there are more rows, it goes to the next page.

Pretty simple :
var query = /* your Linq query */;
query = query.Skip(itemsPerPage * pageIndex).Take(itemsPerPage);

You will have a lot more luck finding the answer, if you say paging.
this is one approach
Asp.net mvc isn't like the regular asp.net, where you can get by without knowing a bit more of the underlying stuff.
Also note, that how exactly you implement paging in the controller & get the set of page links, depends on how you are getting a hold of your data.

Related

Approach to implement lazyloading with JqGrid

I have a data source with over 10,000 records. And I want to implement lazy loading with JqGrid.
This is my approach:
Back-end: SQL stored procedure to select data by page, page size, where expression and sort(order by) expression.
Font-end: send request to get data to Web Method (web service) with: page, page size, where expression, sort expression.
JqGrid: load data to jqGrid
Here is the main issue:
- I can't overwrite the page count, records count of jqGrid. e.g. The pager info always display [page 1 of 1] and [records 1-50 of 50] if page = 1 and page size = 50.
- Thus, I can't use the paging button of jqGrid
I thought about implement custom paging infor (pages, records, paging button) but it will take time and effort. So it's better to reuse jqGrid paging, isn't it?
Does this approach good enough? Or what I need to improve?
Update: I tried to implement json reader at Can I implement lazy loading with jqGrid? but no luck.
If I need to update any information, please tell me.
Thanks in advance.

asp.net + Binding DataSet to already bounded Listview

Basically what I want to achieve here is to show more items in batches just like the YouTube comments session in every vid, about 10 comments shows up for the start with a "Show More" button below which loads additional comments in batches of 10 when clicked upon. Anyone has a clue of how to go about this?
Not sure if this is right but what I intend to do now is to bind additional records to my Listview again with DataSets without extracting the data that has already been bonded.
Currently I am binding my first set of records with a Dataset filled with data from my SQL Server like this:
DataSet ds = activityBll.GetActivityAttendees(activityId);
DataTable dt = ds.Tables[0];
uilvEventParticipants.DataSource = dt;
uilvEventParticipants.DataBind();
The simplest way to do this would be to do as you suggest, and just keep getting more records from farther and farther in the past. You'll experience increasing delays as you rebind, and if you allow users to make changes to comments you'll have to do a huge postback to process those. If you go this way you should look at UpdatePanels.
The most elegant would involve jQuery or a similar Ajax technology to fetch additional records and add them to the bottom of an HTML table (not necessarily a ListView) without refreshing the page. There are quite a few questions on this board about doing that.

Efficient paging with Repeater, SQL Server and custom Pager

I have just uploaded the following article to codeproject:
http://www.codeproject.com/KB/webforms/efficientpagingrepeater.aspx
Basically it is using Repeater, SQL Server with the ROW_NUMBER() OVER statement and a custom pager.
I would like to extend the pager so that it can be used multiple times on a single page and also allow previous / next buttons. I am unsure how to do that - can anybody provide suggestions / some code modifications?
CP hasn't posted the article yet, so I cannot comment on that. I can say that just about every DAL tool (EF, NH, AR, Massive, Dapper.Net, Simple.Data) all have paging built in. so hooking up paging to a repeater should not be a problem at all.
If the article is referring to a webforms server control that pages data, I would avoid it at all costs. data access should not be managed by UI components. and using any of the various DAL listed above, it's very simple to access the database using code, instead of drag-n-drop controls.
to get db paging in place you need 3 inputs and 2 outputs
inputs
sql query & parameters
starting point (page or page index)
max # of records (page size)
outputs
current page of results
total number of records
using the total number of records and the page size you can calculate the total number of pages.
var pages = total records / page size + (total records % page size > 0 ? 1 : 0);
with the page of results, current page & total number of pages you can build the UI layout
I hope this resolves your question.

ASP.NET DataGridView paging with server-sided paging

I am passing the PageNumber and PageSize to a stored procedure, which returns only that page of data. It also returns a record count what the total number of records would be if I returned all of them at once.
Generally, how do i hook this up to the DataGridView to enable paging?
It seems like the expectation is for the resultset to contain the complete dataset.
Many of the properties that I expect to be able to set, like RecordCount, appear to be read only.
Could someone give me general pointers?
I created a similar solution once, and adjusted a GridView (not sure if you mean a DataGrid or a GridView) to use server-side paging. The code is too much to post here and since there's no option for attachments here, you can download the code from http://www.raskenlund.com/downloads/GridView.zip
Check out the following article which describes the same thing:
http://www.highoncoding.com/Articles/210_GridView_Custom_Paging.aspx

asp.net custom datapager

in all the datapager examples i've seen (using a LinqDataSource tied to a ListView) the queries to the database return the full recordset. how can i get JUST the page of rows that i want to display?
for example, if i have a table with 1million rows and i want to see the 10 results on page 5, i would be getting rows 51 to 60.
i'm sure i have to implement custom paging but i haven't found any good examples.
There are many ways of doing this, however, I personally like a SQL based solution that goes to the database and gets the result set. This 4GuysFromRolla Article does a good job of explaining it.
If you're using MSSql2005, take a look at this article.
As you can see, the trick is to use the function ROW_NUMBER(), that allow you to get the sequential number of a row in a recordset. With it you can simply enable pagination based upon the number of rows you want to get in a page.
I was under the impression (from Scott Guthie's blog post "LINQ to SQL (Part 9)") that the LinqDataSource handles the paging for you at the database level:
One of the really cool things to notice above is that paging and sorting still work with our GridView - even though we are using a custom Selecting event to retrieve the data. This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient).
(original emphasis)
If you are using some custom paging, you can do something like this in LINQ to SQL:
var tagIds = (from t in Tags where tagList.Contains(t.TagText) select t.TagID).Skip(10).Take(10).ToList();
This is telling LINQ to take 10 rows (equivalent to T-SQL "TOP 10"), after skipping the first 10 rows - obviously these values can be dynamic, based on the Page Size and page number, but you get the idea.
The referenced post also talks about using a custom expression with a LinqDataSource.
Scott has more information on Skip/Take in Part 3 as well.

Resources