Basscyst
04-18-2008, 07:28 PM
I'm still new to ASP.net so if this seems senseless please let me know why. I needed this because I wanted to page non-tabular data. So the grid view seemed an illogical choice. If anything, this has been a great learning experience.
Using the following code as a reference, I was able to make this work.
http://aspnet.4guysfromrolla.com/articles/081804-1.aspx
I altered the code a bit and came up with the following class:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Paging
/// </summary>
public class Paging
{
DataTable getData(String SQLStr)
{
DataSet DS = new DataSet();
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["XSSolutionsConnectionString"].ConnectionString);
SqlDataAdapter objSQLAdp = new SqlDataAdapter();
objSQLAdp.SelectCommand = new SqlCommand(SQLStr, Conn);
objSQLAdp.Fill(DS, "Comments");
return DS.Tables["Comments"];
}
public String SQLStr;
public Label PageNumberLabel;
public Button NextButton;
public Button BackButton;
public Repeater bindObj;
public Label PageNoCurrent;
public int PerPage;
public void setPaging()
{
// Populate the repeater control with the Items DataSet
PagedDataSource PD = new PagedDataSource();
PD.DataSource = getData(SQLStr).DefaultView;
PD.AllowPaging = true;
PD.PageSize = PerPage;
int prsPageNo = Convert.ToInt32(PageNoCurrent.Text);
PD.CurrentPageIndex = prsPageNo;
PageNumberLabel.Text = "Page: " + (Convert.ToInt32(PageNoCurrent.Text) + 1).ToString() + " of " + PD.PageCount.ToString();
// Disable Prev or Next buttons if necessary
BackButton.Enabled = !PD.IsFirstPage;
NextButton.Enabled = !PD.IsLastPage;
bindObj.DataSource = PD;
bindObj.DataBind();
}
public void moveNext()
{
String txtPageNo = PageNoCurrent.Text;
int prsPageNo = Convert.ToInt32(txtPageNo);
PageNoCurrent.Text = (prsPageNo + 1).ToString();
}
public void moveBack()
{
String txtPageNo = PageNoCurrent.Text;
int prsPageNo = Convert.ToInt32(txtPageNo);
PageNoCurrent.Text = (prsPageNo - 1).ToString();
}
}
Now my question is, in the class above I pass the next and back buttons. Is it possible upon creation of a new Paging(), to set moveNext() and moveBack() as click events on the appropriate buttons?
Currently I have to create a separate function for back and next like so:
public void pageBack1 (object sender, EventArgs e)
{
PG.moveBack();
PG.setPaging();
}
public void pageNext1(object sender, EventArgs e)
{
PG.moveNext();
PG.setPaging();
}
And then set the event handles manually on the buttons. It'd be real nice if it could just apply this when the class is started.
Hope this all makes sense,
Basscyst
Using the following code as a reference, I was able to make this work.
http://aspnet.4guysfromrolla.com/articles/081804-1.aspx
I altered the code a bit and came up with the following class:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Paging
/// </summary>
public class Paging
{
DataTable getData(String SQLStr)
{
DataSet DS = new DataSet();
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["XSSolutionsConnectionString"].ConnectionString);
SqlDataAdapter objSQLAdp = new SqlDataAdapter();
objSQLAdp.SelectCommand = new SqlCommand(SQLStr, Conn);
objSQLAdp.Fill(DS, "Comments");
return DS.Tables["Comments"];
}
public String SQLStr;
public Label PageNumberLabel;
public Button NextButton;
public Button BackButton;
public Repeater bindObj;
public Label PageNoCurrent;
public int PerPage;
public void setPaging()
{
// Populate the repeater control with the Items DataSet
PagedDataSource PD = new PagedDataSource();
PD.DataSource = getData(SQLStr).DefaultView;
PD.AllowPaging = true;
PD.PageSize = PerPage;
int prsPageNo = Convert.ToInt32(PageNoCurrent.Text);
PD.CurrentPageIndex = prsPageNo;
PageNumberLabel.Text = "Page: " + (Convert.ToInt32(PageNoCurrent.Text) + 1).ToString() + " of " + PD.PageCount.ToString();
// Disable Prev or Next buttons if necessary
BackButton.Enabled = !PD.IsFirstPage;
NextButton.Enabled = !PD.IsLastPage;
bindObj.DataSource = PD;
bindObj.DataBind();
}
public void moveNext()
{
String txtPageNo = PageNoCurrent.Text;
int prsPageNo = Convert.ToInt32(txtPageNo);
PageNoCurrent.Text = (prsPageNo + 1).ToString();
}
public void moveBack()
{
String txtPageNo = PageNoCurrent.Text;
int prsPageNo = Convert.ToInt32(txtPageNo);
PageNoCurrent.Text = (prsPageNo - 1).ToString();
}
}
Now my question is, in the class above I pass the next and back buttons. Is it possible upon creation of a new Paging(), to set moveNext() and moveBack() as click events on the appropriate buttons?
Currently I have to create a separate function for back and next like so:
public void pageBack1 (object sender, EventArgs e)
{
PG.moveBack();
PG.setPaging();
}
public void pageNext1(object sender, EventArgs e)
{
PG.moveNext();
PG.setPaging();
}
And then set the event handles manually on the buttons. It'd be real nice if it could just apply this when the class is started.
Hope this all makes sense,
Basscyst