hello all;
I am programming a webpage with .NET C# in which I want to display data from database and I am using custom paging
* (page size = 4)
* I am using imageuttons to navigate (next,prv,first,last) in which I make the decrement or increment but next will only take me to page no. 2, and prv takes me to page no 1. no matter where I am at that point
and here is the code for my entire page because I can't figure where the mistake occurs exactly -
using System;
-
using System.Collections;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Web;
-
using System.Web.SessionState;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.HtmlControls;
-
using MySql.Data.MySqlClient;
-
using MySql.Data.Types;
-
-
namespace Yacoup
-
{
-
/// <summary>
-
/// Summary description for workgroup.
-
/// </summary>
-
public class workdetails : System.Web.UI.Page
-
{
-
protected System.Web.UI.WebControls.Label result;
-
protected System.Web.UI.WebControls.Label no1txt;
-
protected System.Web.UI.WebControls.Label no2txt;
-
protected System.Web.UI.WebControls.Label no3txt;
-
protected System.Web.UI.WebControls.Label no4txt;
-
protected System.Web.UI.WebControls.ImageButton first;
-
protected System.Web.UI.WebControls.ImageButton previous;
-
protected System.Web.UI.WebControls.ImageButton last;
-
protected System.Web.UI.HtmlControls.HtmlTable resultt;
-
protected System.Web.UI.HtmlControls.HtmlTableCell TD7;
-
protected System.Web.UI.HtmlControls.HtmlTableCell TD6;
-
protected System.Web.UI.HtmlControls.HtmlTableCell td00;
-
protected System.Web.UI.HtmlControls.HtmlTableCell td10;
-
protected System.Web.UI.HtmlControls.HtmlTableCell td20;
-
protected System.Web.UI.HtmlControls.HtmlTableCell td30;
-
protected System.Web.UI.HtmlControls.HtmlTableCell TD3;
-
public int pcount=0;
-
public int pagesize=4;
-
public int npage=1;
-
public int srow,erow,rcount;
-
private DataSet ds=new DataSet();
-
private MySqlConnection con=new MySqlConnection("Server=localhost;Database=yacoup;Uid=root;Pwd=root;");
-
private MySqlDataAdapter da=new MySqlDataAdapter();
-
string TableName="employee";
-
string actionpg="workdetails.aspx";
-
protected System.Web.UI.WebControls.Table Table1;
-
protected System.Web.UI.WebControls.HyperLink h1;
-
protected System.Web.UI.WebControls.HyperLink h2;
-
protected System.Web.UI.WebControls.HyperLink h3;
-
protected System.Web.UI.WebControls.HyperLink h4;
-
protected System.Web.UI.WebControls.HyperLink h5;
-
int xc=0;
-
int i;
-
protected System.Web.UI.WebControls.ImageButton next;
-
-
-
-
-
-
private void Page_Load(object sender, System.EventArgs e)
-
{
-
-
-
h1.NavigateUrl="workdetails.aspx?s=1";
-
h2.NavigateUrl="workdetails.aspx?s=2";
-
h3.NavigateUrl="workdetails.aspx?s=3";
-
h4.NavigateUrl="workdetails.aspx?s=4";
-
h5.NavigateUrl="workdetails.aspx?s=5";
-
-
i = Convert.ToInt32("0"+Request.QueryString["s"]);
-
-
-
-
TareefInit(i,npage);
-
}
-
-
-
-
private void TareefInit(int x,int page)
-
{
-
#region ProcessInit
-
-
if(page==0)
-
page=1;
-
-
-
if(x==5)
-
{da=new MySqlDataAdapter("select * from employee order by empid asc ",con);}
-
else
-
{da=new MySqlDataAdapter("select * from employee order by expyear desc",con);}
-
da.Fill(ds,TableName);
-
-
rcount=Convert.ToInt32(ds.Tables[TableName].Rows.Count);
-
-
if(rcount%pagesize==0)
-
{
-
pcount=rcount/pagesize;
-
-
}
-
else
-
{
-
pcount=rcount/pagesize;
-
pcount++;
-
-
-
}
-
-
-
#endregion
-
TareefSERow();
-
TareefShow();
-
-
-
}
-
-
private void TareefSERow()
-
{
-
#region startrow and endrow
-
if(npage==0 || npage==1)
-
{
-
srow=0;
-
erow=pagesize;
-
if(rcount<4)
-
erow=rcount;
-
//Response.Write(rcount+"<-");
-
-
}
-
else
-
{
-
int mpage = npage;
-
srow=(mpage-1)*pagesize;
-
erow=npage*pagesize;
-
if(erow>rcount)
-
erow=rcount;
-
if(srow<0)
-
srow=0;
-
}
-
#endregion
-
}
-
-
-
private void TareefShow()
-
{
-
xc=0;
-
#region Show Database Result
-
for(int i=srow;i<erow;i++)
-
{
-
xc++;
-
if(xc==1)
-
{
-
//cell2
-
no1txt.Text=" ";
-
no1txt.Text+="<font color='#336699'>Employee Name: </font>"+ds.Tables[TableName].Rows[i][1];
-
no1txt.Text+="<font color='#336699'><br> Employee Work: </font>"+ds.Tables[TableName].Rows[i][2];
-
-
}
-
if(xc==2)
-
{
-
-
//cell2
-
no2txt.Text=" ";
-
no2txt.Text+="<font color='#336699'>Employee Name: </font>"+ds.Tables[TableName].Rows[i][1];
-
no2txt.Text+="<font color='#336699'><br> Employee Work: </font>"+ds.Tables[TableName].Rows[i][2];
-
-
}
-
-
if(xc==3)
-
{
-
-
//cell2
-
no3txt.Text=" ";
-
no3txt.Text+="<font color='#336699'>Employee Name: </font>"+ds.Tables[TableName].Rows[i][1];
-
no3txt.Text+="<font color='#336699'><br> Employee Work: </font>"+ds.Tables[TableName].Rows[i][2];
-
-
-
}
-
-
if(xc==4)
-
{
-
-
//cell2
-
no4txt.Text=" ";
-
no4txt.Text+="<font color='#336699'>Employee Name: </font>"+ds.Tables[TableName].Rows[i][1];
-
no4txt.Text+="<font color='#336699'><br> Employee Work: </font>"+ds.Tables[TableName].Rows[i][2];
-
}
-
-
-
result.Text="Page :"+npage+" out "+pcount;
-
-
}
-
-
#endregion
-
-
TareefClear();
-
}
-
-
private void TareefClear()
-
{
-
int z=erow-srow;
-
-
for(int y=4;y>z;y--)
-
{
-
#region TareefRemoveControls
-
if(y==4)
-
{
-
td30.Controls.Clear();
-
td30.Attributes.Clear();
-
}
-
if(y==3)
-
{
-
td20.Controls.Clear();
-
td20.Attributes.Clear();
-
}
-
if(y==2)
-
{
-
td10.Controls.Clear();
-
td10.Attributes.Clear();
-
}
-
if(y==1)
-
{
-
td00.Controls.Clear();
-
td00.Attributes.Clear();
-
}
-
-
#endregion
-
}
-
-
if(no1txt.Text=="")
-
{
-
TD3.Controls.Clear();
-
result.Text="No Data Found";
-
}
-
}
-
-
#region Web Form Designer generated code
-
override protected void OnInit(EventArgs e)
-
{
-
//
-
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
-
//
-
InitializeComponent();
-
base.OnInit(e);
-
}
-
-
/// <summary>
-
/// Required method for Designer support - do not modify
-
/// the contents of this method with the code editor.
-
/// </summary>
-
private void InitializeComponent()
-
{
-
this.Load += new System.EventHandler(this.Page_Load);
-
}
-
#endregion
-
-
public void nxt_prv(object sender, CommandEventArgs x)
-
{
-
-
if (x.CommandName == "next" && x.CommandArgument.ToString() == "next")
-
{
-
if(!(npage>=pcount))
-
{
-
npage++;
-
-
-
ds.Clear();
-
-
TareefInit(i,npage);
-
-
-
}
-
-
-
}
-
else if(x.CommandName == "prv" && x.CommandArgument.ToString() == "prv")
-
{
-
if(!(npage<=1))
-
{
-
npage++;
-
ds.Clear();
-
-
TareefInit(i,npage);
-
}
-
}
-
else if(x.CommandName == "first" && x.CommandArgument.ToString() == "first")
-
{
-
npage=1;
-
-
ds.Clear();
-
-
TareefInit(i,npage);
-
}
-
-
else if(x.CommandName == "last" && x.CommandArgument.ToString() == "last")
-
{
-
-
npage=pcount;
-
ds.Clear();
-
TareefInit(i,npage);
-
}
-
-
}
-
-
private void next_Click(object sender, System.Web.UI.ImageClickEventArgs e)
-
{
-
-
}
-
-
private void previous_Click(object sender, System.Web.UI.ImageClickEventArgs e)
-
{
-
-
}
-
-
private void first_Click(object sender, System.Web.UI.ImageClickEventArgs e)
-
{
-
-
}
-
-
private void last_Click(object sender, System.Web.UI.ImageClickEventArgs e)
-
{
-
-
}
-
-
-
-
-
-
}
-
}
-
-
5 1227
Have you considered using a DataGrid? If you enable paging for your datagrid in the Property Builder, you'll still have paging functionality and avoid lots of debugging headaches.
hello
You said you want to display data from database .but from your code i dint get where you are displaying your data.
if its grid you can have display it with paging option and if any reports you are using for display then you have properties directly
hello all
thank u for replying to my question, as for my data I am showing it in the function TareefShow(), using labels we used this way because we thought it would be the best way to control the way it will appear on the screen.
as for my question that when I click the next button or any other button the value of (npage//the page we r standing in at that point) wont keep the change it will always go back to the initial value which is 1.
now how we solved that problem
we used cache object to save the change in the npage with each click and here is the code with some explanation I hope it is helpful -
-
public void nxt_prv(object sender, CommandEventArgs x)
-
{
-
if (x.CommandName == "next" && x.CommandArgument.ToString() == "next")
-
{
-
-
if(!(npage>=pcount)) //pcount: No. of pages ,npage: the page we r showing now
-
{
-
//npagecache: the cache opject to save the value of npage in
-
if (Cache.Get("npagecache") != null)
-
{
-
// we retreive the value from the npagecache and asign it to n page
-
npage = Convert.ToInt32( Cache.Get("npagecache"));
-
}
-
npage++;
-
/*after the increment to npage we stored the new value in npagecache to be able to retreive this value as needed in the program */
-
Cache.Insert("npagecache",npage);
-
ds.Clear();
-
TareefInit(i,npage);
-
}
-
}
-
-
else if(x.CommandName == "prv" && x.CommandArgument.ToString() == "prv")
-
{
-
if (Cache.Get("npagecache") != null)
-
{
-
npage = Convert.ToInt32( Cache.Get("npagecache"));
-
}
-
if(!(npage<=1))
-
{
-
npage--;
-
Cache.Insert("npagecache",npage);
-
ds.Clear();
-
TareefInit(i,npage);
-
}
-
}
-
else if(x.CommandName == "first" && x.CommandArgument.ToString()== "first")
-
{
-
if (Cache.Get("npagecache") != null)
-
{
-
npage = Convert.ToInt32( Cache.Get("npagecache"));
-
}
-
npage=1;
-
Cache.Insert("npagecache",npage);
-
ds.Clear();
-
TareefInit(i,npage);
-
}
-
-
else if(x.CommandName == "last" && x.CommandArgument.ToString() == "last")
-
{
-
if (Cache.Get("npagecache") != null)
-
{
-
npage = Convert.ToInt32( Cache.Get("npagecache"));
-
}
-
npage=pcount;
-
Cache.Insert("npagecache",npage);
-
ds.Clear();
-
TareefInit(i,npage);
-
}
-
-
}
-
-
and off cores the in page load we used IsPostBack to empty the page for each post back now for the page load code -
private void Page_Load(object sender, System.EventArgs e)
-
{
-
h1.NavigateUrl="workdetails.aspx?s=1";
-
h2.NavigateUrl="workdetails.aspx?s=2";
-
h3.NavigateUrl="workdetails.aspx?s=3";
-
h4.NavigateUrl="workdetails.aspx?s=4";
-
h5.NavigateUrl="workdetails.aspx?s=5";
-
-
i = Convert.ToInt32("0"+Request.QueryString["s"]);
-
-
TareefInit(i,npage);
-
-
if(!Page.IsPostBack)
-
{
-
//When ever the page is redirected the cache opject will be emptied
-
Cache.Remove("npagecache");
-
}
-
}
-
and finally if there is any question or comment please send a reply and if there is any other way to solve the npage problem I would be glad to hear and try it.
Did you consider stashing the page number in a session variable? That may result in less overhead.
I still don't quite get why you chose to not use the DataGrid. It is quite customizable and makes things a whole lot easier for you as a developer. Just some advice for future projects - you can take it or leave it.
thank u alot, but the reason we are not considering the change to a datagrid is that we have more than one page with the same design and we are trying to keep it this way as for the future we will use the datagrid :)
about the session variable we r gonna try it for the page
thank u again
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sarah |
last post by:
I'm having problems getting the FormBuilder package to work.
I've spent the last 3hrs reading what little documentation and
examples I could find and the idea seems great, if only it'll actually...
|
by: Danny |
last post by:
Newbie here
Thanks for the help with this command:
print "Status: 204 No Content\n\n";
which returns nothing in a perl script.
This is great because I just want my perl script to increment a...
|
by: AJ |
last post by:
Have I just wasted money on buying VS.NET Beta 2?
I can run the start page but as soon as I try to load a
solution file, I get an error within the Microsoft
Development Environment as the Common...
|
by: BadPony |
last post by:
Anyone using Peoplesoft on a Federated UDB
(shared nothing)Environment on Open System Platforms?
Preferably AIX, but any war stories would be good.
TEA
EB-C
|
by: Dune |
last post by:
How do I compare a boolean with Nothing?
I can't say "If boolVar Is Nothing" because a boolean is
not a reference type, but "If boolVar = Nothing" always
seems to return true, even if the...
|
by: Steve Last |
last post by:
Hi all,
I’m using IIS6 for our college Intranet and I’m having trouble using
Request.Form. Here is my code:
<%
If Request.QueryString("action") = "show" Then
Response.Write "title: " &...
|
by: Doug Glancy |
last post by:
I got the following code from Francesco Balena's site, for disposing of Com
objects:
Sub SetNothing(Of T)(ByRef obj As T)
' Dispose of the object if possible
If obj IsNot Nothing AndAlso...
|
by: andmarti |
last post by:
If I build a strict with:
import struct
print struck.pack ('i', 1)
it returns a '\n'.
What's wrong with it???
:(
--
|
by: andreas |
last post by:
Can someone tell me
1)
Is there a difference between closing or disposing a reference data type
2)
about nothing
dim fr as form2
isnothing(fr) gives true
|
by: optimistx |
last post by:
I am trying to learn, which type of javascript code is really good according
to the experts of this newsgroup. It might be easy to show few lines of code
with nothing to blame, but what about...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |