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)
-
<asp:Repeater Id="myRepeater" runat="server">
-
<ItemTemplate>
-
<div>
-
<h4>
-
<%# Eval("NewsTitle") %>
-
<a href=NewsDetails.aspx?ID="<%# Eval("id") %>" >
-
more
-
</a>
-
</h4>
-
</div>
-
</ItemTemplate>
-
</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....
-
'....
-
mySqlDataReader= sqlcom.ExecuteReader
-
DataTable dt = new DataTable();
-
dt.Load(mySqlDataReader);
-
myRepeater.DataSource = dt;
-
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:
-
'....
-
mySqlDataReader= sqlcom.ExecuteReader
-
DataTable dt = new DataTable();
-
dt.Load(mySqlDataReader);
-
DataView dv = new DataView(dt);
-
myRepeater.DataSource = dv;
-
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