Solved

Display records in blocks in gridview?

Posted on 2011-09-03
21
396 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:MyersA
  • 10
  • 8
  • 3
21 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
Comment Utility
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.
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
Comment Utility
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...
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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.
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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 ="" />
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 200 total points
Comment Utility
Hello, google is friend:
http://www.google.com/search?aq=1&oq=paging+with+ajax&gcx=c&sourceid=chrome&ie=UTF-8&q=paging+with+ajax+in+asp+net

I like this example that implement paging by using AJAX:
http://www.asp.net/ajax/videos/how-do-i-implement-the-ajax-paging-pattern

Related to your other question, is of my knowledge that __EVENTTARGET, __EVENTARGUMENT are reserved fields used by asp.net, I don't known what happen if you play with this.
0
 
LVL 4

Assisted Solution

by:Friman001
Friman001 earned 200 total points
Comment Utility
What you are looking for is the use of the Server Explorer to create your gridview.  Click View > Server Explorer.  In this, you will need to set up your connection to your SQL database by clicking the icon for "Connect to Database" and then you will need to enter the correct information with the username and password if it is secured.  After that, find the table that you would be wanting to pull the data from by opening your database connection in the Server Explorer (after you added it of course!), expanded the Tables folder, and then drag your table into your Webform (.aspx page) and a gridview will be created.  The GridView tasks window should open, but if it doesn't, select the gridview and click the arrow that shows up on the top right cornor of the gridview.  There, will see options for paging, sorting, editing, deleting, and selection.  Hope this helps!!!
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
Sorry it is all in one big lump and not seperated into paragraphs!
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
You can also click the "Auto Format" at the top of the GridView Tasks dialog box to change the look of the GridView.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
Where my comments what you were looking for?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:Friman001
Comment Utility
Were*
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
@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.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
@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?
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
Just dynamically add more rows the the grid.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
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.
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
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.
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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.
0
 
LVL 4

Expert Comment

by:Friman001
Comment Utility
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!
0
 
LVL 2

Author Closing Comment

by:MyersA
Comment Utility
thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now