Link to home
Start Free TrialLog in
Avatar of MyersA
MyersA

asked on

Display records in blocks in gridview?

I would like to be able to display records on my gridview in blocks.

So when the page loads, it will display 20 records, and at the bottom of the grid, there will be a button or link that says "Display more records". When I click on that, it will display another 20 records and now I will have 40 records displayed on my webform.

How can this be done? The gridview is currently databound to a dataview.

Thanks.
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Yes, you can, the better way is to control the rows returned for your query, you can do someting like this in your DB side (this syntax is supported since SQL Server 2005):
SELECT TOP (@pTop)
	*
FROM
	dbo.MyTable

Open in new window

So @pTop can be a parameter of your query or sp, then in your aspx page you can have a parameter starting with the value 20, then when you click the "Display more records" button you can increment this value (+20) that will be passed to your query or sp as a parameter.

Do you have the idea? if not, feel free in ask.

I hope this help.
Avatar of MyersA
MyersA

ASKER

THanks for the post.

The problem is that it may start at 20 records, but if the user decides to view all the records, the gridview may end up being bound to 1000 records, which will be quite a burden. Essentially, I would be doing a gridview.bind for 20, 40, 60,...1000, 1020 records, and so on, right?

I will be displaying friendbook friends, and it's not unusual for a member to have thousands of friends. So binding my gridview to a 5,000-record dataview will be tough.

facebook has this same functionality, and I'm certain they don't do a round-trip to the server every time someone wants to look at a few more records. How do they do it?
Well buddy, you need to combine techniques, from server logic to client logic, is hard to me give you a direct solution for that, because it depends of your project requirements, data structure, etc...

I can give you this idea, you can do a server logic that sent to the client a limited result-set at first (again this depends in how yours members frequently access this data), save it in a dynamic array on your client browser (by using JASON, AJAX technologies), then handle your display logic that will consume this data, if more data is required you need to do another round-trip to the server and sent more records, all this in a way that avoid several round-trips to the server.

I hope this is useful for you...
Avatar of MyersA

ASKER

Would you happen to know of any links that can possibly point me to the right direction?

Out of curiosity, and this is the reason I posted this question (but it is totally unrelated), would you happen to know why gridview.OnPageIndexChanging doesn't fire with __EVENTTARGET, __EVENTARGUMENT hidden fields?

I'll understand if you don't want to reply to this question.

Thanks.
Avatar of MyersA

ASKER

If I add the following two fields, gridview.OnPageIndexChanging doesn't fire. Otherwise, it does.

<input type ="hidden" name ="__EVENTTARGET" value ="" />
<input type ="hidden" name ="__EVENTARGUMENT" value ="" />
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry it is all in one big lump and not seperated into paragraphs!
You can also click the "Auto Format" at the top of the GridView Tasks dialog box to change the look of the GridView.
Where my comments what you were looking for?
Were*
Avatar of MyersA

ASKER

@Friman001: Thanks for the comments.

You're explaining how to create a gridview with a data source. I am finding a way to display records in a gridview in blocks, similar to facebook's scrolling function.
@MyersA: That method will allow you to enable paging, which was what I got out of your post.  Display 20 records and have an option to click a page number to display the next 20 records.

Is that not what you were asking about?
Avatar of MyersA

ASKER

I know how to create a gridview that allows paging.

What I wanted to do was that when the page loads, it will display 20 records, and at the bottom of the grid, there will be a button or link that says "Display more records". When I click on that, it will display another 20 records and now I will have 40 records displayed on my webform.

If you have a facebook profile, you'll notice that you will initially see X records. When you click on the link on the bottom it will display 20 more records without posting back.

It seems this can be done with json and web services, but I ended going another direction.
Just dynamically add more rows the the grid.
Sorry for messing up your post.  I think you should be able to change the amount of records in the view from the code behind file.  Add a button at the bottom, so when you click it, it will add 20 more records to show up in the GridView.  I do not know if this can be done but, if you can set how many show up at first through the properties of the GridView, then you can dynamically change it when a button is clicked.
Avatar of MyersA

ASKER

THanks for the reply.

The problem there is that it will have to go back to the server, and that's the issue I want to avoid. This can be done with javascript, but it's a more complicated solution. IF you go to facebook, you'll notice that facebook will continue showing friends when you scroll down. And it never refreshes.
If you want a button that you can click to load more data, just add a method to the code behind file.  When you click the button, have it create more rows in the codebehind file, and then add them to the gridview.  I actually have a site that inserts labels and textboxes into a table (which you could do too) when you click the button.

Just create a table, add a row using that table id in the code behind file, insert a cell into that tablerow and add things to that cell.  If this pertains to you, let me know and I can give you examples.  If you never want to make a call back to the database server, if that is what you are using, then you will have to load all the data into a datatable and make calls as you need it.
Avatar of MyersA

ASKER

That way, the webform wil go back to the server and then it will post back. It will do a refresh. That's not how I want to do it. And I don't want to load all the data into the table.

There is a way to do this, but it involves using json and webservices.
That is out of my area.  I have never heard of json.  I have heard of webservies but, I have never used them.  Try Google'ing if you already know what you are looking for or wait for someone else to come along to the thread.

Sorry I could not help!
Avatar of MyersA

ASKER

thanks.