Hi,
I have created some custom class objects.
public class astroObjects
{
public int Id { get; set; }
public String Name { get; set; }
public String Desc { get; set; }
public int CatId { get; set; }
public String ImageThumb { get; set; }
}
My data is stored in an SQL database.
When I want to display the data I query the database and make a list as so.
public static List<astroObjects> GetAstroObjects(int catId)
{
List<astroObjects> astroobjects = new List<astroObjects>();
// Do all sql work including setting up connection, command, ect.
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
String mySQL;
int thecatid = catId;
mySQL = "SELECT objectName, object_id, Desc, objectThumbnail WHERE CatId = @catId ORDER BY objectName";
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand(mySQL, conn);
comm.Parameters.Add("@catid", System.Data.SqlDbType.Int);
comm.Parameters["@catid"].Value = thecatid;
conn.Open();
reader = comm.ExecuteReader();
while (reader.Read())
{
astroObjects c = new astroObjects();
c.Name = reader["ObjectName"].ToString();
c.Desc = reader["Desc"].ToString();
c.Id = Convert.ToInt16( reader["object_id"]);
c.ImageThumb=reader["ObjectThumbnail"].ToString();
astroobjects.Add(c);
}
return astroobjects;
}
I next want to bind this list to a datalist.
Most examples of paging to datalist on the web I have seen are using dataset but I read this old article online http://www.dotnetcurry.com/ShowArticle.aspx?ID=345 that says you can just bind
to a pageddatasource so have done the following:
protected void getTheData(int catid)
{
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
page.DataSource = myproject.astroObjects.GetAstroObjects(catid);
page.PageSize = 10;
page.CurrentPageIndex = CurPage;
Decimal intNumRecs;
intNumRecs = 50;//need to replace with actual number of records returned in the query
Decimal numPages;
numPages = Math.Ceiling(intNumRecs / page.PageSize);
int strCurPage;
strCurPage = Convert.ToInt16(CurPage) + 1;
LabelCurrentPage.Text = "Pg: " + strCurPage + " of " + numPages + " (" + intNumRecs + " objects)";
ButtonBack.Visible = (!page.IsFirstPage);
ButtonNext.Visible = (!page.IsLastPage);
DataList1.DataSource = page;
DataList1.DataBind();
}
and here's the code for my next and previous buttons
public void Next_Click(Object obj, EventArgs e)
{
//identify current datafilter
CurPage += 1;
getTheData(Convert.ToInt16(ViewState["DataFilterId"]));
}
public void Prev_Click(Object obj, EventArgs e)
{
CurPage -= 1;
getTheData(Convert.ToInt16(ViewState["DataFilterId"]));
}
and here's my page load code
protected void Page_Load(object sender, EventArgs e)
{
checkloggedin();
if (!IsPostBack) //if page loaded not for first time
{
getAstroObjectsById(0); //set default recommedations filter to other related products
CurPage = 0;
}
if (IsPostBack) //if page loaded for first time
{
CurPage = Convert.ToInt32(ViewState["CurPage"]);
}
}
Here's how I set CurPage variable (Current page)
public int CurPage
{
get
{
if (this.ViewState["CurPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurPage"].ToString());
}
set
{
this.ViewState["CurPage"] = value;
}
}
and here's how I handle DataFilterId variable (category in the query)
public int DataFilterId
{
get
{
if (this.ViewState["DataFilterId"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["DataFilterId"].ToString());
}
set
{
this.ViewState["DataFilterId"] = value;
}
}
When I visit the page 10 records are displayed as expected. However when I click the next button the next 10 records aren't shown but the right page number is.
I can't figure why this isn't working.
The article uses Linq and I want to just do an SQL query as in the my list code above. I need to just query database so just the amount of records I need are retrieved from the database and added to the list.
Hope some one can help me.
Cheers
Mark :)