I have two repeaters on my page. One for displaying data from a database and the other for holding pagenumbers as linkbuttons which allow navigation through the data.
This seems to work fine. However, I also have three combo boxes which control how the data is sorted. When the combo box is changed I call the DataLoad method which should refresh the PagedDataSource with the new sort details.
However, it doesnt work unless you click on a page number at which point it updates.
www.anon.co.uk/?cat=MusicAs you can see, moving around pages works but if you change the combo boxes - nothing happens until you click on a page number.
The code I am using:
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Pieces : System.Web.UI.Page
{
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand +=
new RepeaterCommandEventHandler(rptPages_ItemCommand);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string check = Request.QueryString["cat"];
if (check != null)
{
LoadDataCat();
}
}
}
private void LoadDataCat()
{
string cat = Request.QueryString["cat"];
if (cat != null)
{
cat.Trim();
cat.Replace("'", "''");
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/test");
System.Configuration.ConnectionStringSettings connString;
connString = rootWebConfig.ConnectionStrings.ConnectionStrings["ConnectionString"];
SqlConnection cn = new SqlConnection(connString.ConnectionString);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("CatSearch", cn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(new SqlParameter("@cat", SqlDbType.VarChar, 40));
da.SelectCommand.Parameters["@cat"].Value = cat;
da.SelectCommand.Parameters.Add(new SqlParameter("@RowCount", SqlDbType.Int, 4));
da.SelectCommand.Parameters["@RowCount"].Direction = ParameterDirection.Output;
DataTable dt = new DataTable();
da.Fill(dt);
cn.Close();
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dt);
dv.Sort = getSort();
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = Convert.ToInt32(DropDownList3.Text);
pgitems.CurrentPageIndex = PageNumber;
if (!pgitems.IsLastPage)
{
Label2.Text = "Displaying items " + ((pgitems.PageSize * (PageNumber + 1)) - (pgitems.PageSize - 1)) + " - " + (pgitems.PageSize * (PageNumber + 1)) + " from " + da.SelectCommand.Parameters[1].Value;
}
else
{
Label2.Text = "Displaying items " + ((pgitems.PageSize * (PageNumber + 1)) - (pgitems.PageSize - 1)) + " - " + da.SelectCommand.Parameters[1].Value + " from " + da.SelectCommand.Parameters[1].Value;
}
if (pgitems.PageCount > 1)
{
rptPages.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
}
else
rptPages.Visible = false;
PageNumber = 0;
rptItems.DataSource = pgitems;
rptItems.DataBind();
}
}
void rptPages_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
string check = Request.QueryString["cat"];
if (check != null)
{
LoadDataCat();
}
}
string getSort()
{
string sort = "fundraiserReserve ASC";
switch (DropDownList1.Text)
{
case "Name":
sort = "artistName";
break;
case "Fundraisers Reserve":
sort = "fundraiserReserve";
break;
case "Normal Retail Price":
sort = "retailPrice";
break;
}
switch (DropDownList2.Text)
{
case "Ascending":
sort += " ASC";
break;
case "Descending":
sort += " DESC";
break;
}
return sort;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string check = Request.QueryString["cat"];
if (check != null)
{
LoadDataCat();
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string check = Request.QueryString["cat"];
if (check != null)
{
LoadDataCat();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string check = Request.QueryString["cat"];
if (check != null)
{
LoadDataCat();
}
}
}
}
Any help would really be appreciated. Thanks!