473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How can I do Paging in my websites ?

Hi ,

I am using C# & asp.net in programming , i have problem when display data from tables of DB ,
I am displaying content like this :
Expand|Select|Wrap|Line Numbers
  1.  
  2.  while (mySqlDataReader.Read())
  3.         {
  4. // News is id of div which declared in source .
  5.             News.InnerHtml += " <div>" +"<h4>"+mySqlDataReader["NewsTitle"].ToString()+ 
  6.  "<a href=NewsDetails.aspx?ID=" + mySqlDataReader["id"].ToString() + ">" + "more" +
  7.  "</a>"+
  8.   "</h4>" +
  9.   "</div>";
  10. }
  11.  
so, when i read records from DB , they display in one page !
I need to display 20 records in page 1 , and another 20 records in page 2 ,..etc

How i can do this?

thanx ..
Apr 22 '10 #1
4 2521
semomaniz
210 Expert 100+
Why dont you use a gridview control or a repeater control to display data? Also you will be able to implement paging easily.

Links :
http://lmgtfy.com/?q=gridview+with+paging+c%23

http://lmgtfy.com/?q=repeater+with+paging+c%23
Apr 23 '10 #2
I am using gridview and i know the paging for it , but i need to know the paging when i display data by " innerHtml "

is there a solution for that ??

thank you ..
Apr 23 '10 #3
Is there anyone who can help me ?
Please ;
Apr 28 '10 #4
Frinavale
9,735 Expert Mod 8TB
You can consider using the DataPager class to create a custom paging control. You could also take a look at this MSDN article on Creating a Pager Control for ASP.NET.

But......both of these controls depend on a DataSource that is bound to some control.

In light of you constantly mentioning "innerHtml" I don't think that you are binding to a datasource. So, this means that you are dismissing the tools that are available for you to use to help with Paging.

You don't have to set the innerHTML when you bind to a datasource...instead you would use a template to specify what data is bound to what elements.

This style frees you from hard coding HTML using C#....it separates your user interface (html) from your business logic (decisions made to make the application work) and from your data (datasource)....so it's a lot cleaner and easier to debug.

This means that you don't even have to have the "while (mySqlDataReader.Read())" loop in your code!

You would simply bind the table returned by your sql query execution to the control that is being used to display your data.

You would specify the the "innerHtml" content in the Template design for the control used to display your data.


Here's an example using a Repeater...

This code would go in your ASPX page code (in source view of the page)
Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater Id="myRepeater" runat="server">
  2.   <ItemTemplate>
  3.     <div>
  4.       <h4>
  5.         <%# Eval("NewsTitle") %>
  6.         <a href=NewsDetails.aspx?ID="<%# Eval("id") %>" >
  7.           more
  8.         </a>
  9.       </h4>
  10.     </div>
  11.   </ItemTemplate>
  12. </asp:Repeater>
See how I specified the template that is used to display an item? The <%# %> is used for binding.

See how you don't have to have this in your C# code?

Now lets look at what you would have for your C# code....
Expand|Select|Wrap|Line Numbers
  1. '....
  2.  mySqlDataReader= sqlcom.ExecuteReader
  3.  DataTable dt = new  DataTable();
  4.  dt.Load(mySqlDataReader);
  5.  myRepeater.DataSource = dt;
  6.  myRepeater.DataBind();
Pretty simple eh?
No looping, no "innerHtml" clutter, no hard coded HTML in your business logic...

See how this accomplishes a separation between the UI (user interface) layer and the business logic layer?

Now if you have a bug in your CSS you can just refer to the HTML/ASP code. If you have a bug in your logic, you can just refer to your business logic. You don't get confused by the two because they are in their correct layers/places/files.

It would be better to use a DataView instead of a normal DataTable because the DataView control supports paging!!! And this is what you're looking to do.

So "better" code would be:
Expand|Select|Wrap|Line Numbers
  1. '....
  2.  mySqlDataReader= sqlcom.ExecuteReader
  3.  DataTable dt = new  DataTable();
  4.  dt.Load(mySqlDataReader);
  5.  DataView dv = new DataView(dt);
  6.  myRepeater.DataSource = dv;
  7.  myRepeater.DataBind();
Because the repeater control doesn't have built in paging you would have to use the above mentioned classes to create your pager control. It's pretty nice though because the DataView control will help you a lot when it comes to paging.

-Frinny
Apr 28 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
6
by: Shawn | last post by:
Hi. I already have a datagrid where I'm using paging. I have a stored procedure that fills a temp table with 200-500 rows and then sends back 10 records at the time. When I go to page 2 the SP...
2
by: asad | last post by:
Hello friends, i am designing a ASP.NET page where i want to use custom paging bcoz data is too heavy so pls tell me how can i use custom paging in ASP.NET Thanks
2
by: farhad13841384 | last post by:
Hi , I Hope You fine. I have some problem with this code for paging in asp.net this bottom code work correctly without any error but when I try to place separate code in .VB file then error is...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
6
by: Nathan Sokalski | last post by:
I have tried everything I can think of, I have even tried copying examples from books and websites I have found, but every Formview control that I have tried to create does not show the Paging...
2
by: rn5a | last post by:
In a shopping cart app, a ASPX page retrieves the order details & personal details of a user from a MS-Access database table depending upon the username of the user. The order details of a...
3
by: Ronald S. Cook | last post by:
I was told that if calling lots of records from the database (let's say 100,000), that the GridView's paging feature would automatically "handle" everything. But the 100,000 records are still...
5
by: Donald Adams | last post by:
Hi, I will have both web and win clients and would like to page my data. I could not find out how the datagrid control does it's paging though I did find some sample code that says they do it...
2
by: Vince13 | last post by:
I'm fairly new to c# and Visual Studio. I've been having what seems like a common paging problem. When I try to change pages (even just from the first page to the second), the error comes up: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.