我有一个使用PagedDataSource的DataList控件,页面加载正常,数据显示正常。 我还有另一个DataList,它为分页提供页码。
当用户点击任何页码时,如果回发并增加PagedDataSource CurrentPage属性。
第一次单击它时,此工作正常。页面将重新加载到正确的页面上。但是,再次单击任何页面不会导致回发。页面似乎“闪烁”但是我没有得到服务器端代码停止,不像我第一次点击它。
我认为第一次回发是以某种方式搞砸了回发,但我不知道为什么。
有关于此的任何想法吗?这是页面加载和绑定代码..
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Trace.Write("Before BindItemsList() " + DateTime.Now.ToLongTimeString());
this.BindItemsList();
Trace.Write("After BindItemsList() " + DateTime.Now.ToLongTimeString());
}
}
private void BindItemsList()
{
DataTable dataTable = new DataTable();
if (Cache["dtItems"] != null)
dataTable = (DataTable)Cache["dtItems"];
else
dataTable = this.GetDataTable();
_PageDataSource.DataSource = dataTable.DefaultView;
_PageDataSource.AllowPaging = true;
_PageDataSource.PageSize = 9;
_PageDataSource.CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = _PageDataSource.PageCount;
this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
this.ibtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
this.ibtnNext.Enabled = !_PageDataSource.IsLastPage;
this.ibtnFirst.Enabled = !_PageDataSource.IsFirstPage;
this.ibtnLast.Enabled = !_PageDataSource.IsLastPage;
this.dlProducts.DataSource = _PageDataSource;
this.dlProducts.DataBind();
this.doPaging();
}
protected void ibtnFirst_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = 0;
this.BindItemsList();
}
protected void ibtnPrevious_Click(object sender, ImageClickEventArgs e)
{
CurrentPage -= 1;
this.BindItemsList();
}
protected void ibtnNext_Click(object sender, ImageClickEventArgs e)
{
CurrentPage += 1;
this.BindItemsList();
}
protected void ibtnLast_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
this.BindItemsList();
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
this.BindItemsList();
}
}
/// <summary>
/// Binding Paging List
/// </summary>
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
fistIndex = CurrentPage - 5;
if (CurrentPage > 5)
{
lastIndex = CurrentPage + 4;
}
else
{
lastIndex = 9;
}
if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
{
lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
fistIndex = lastIndex - 9;
}
if (fistIndex < 0)
{
fistIndex = 0;
}
for (int i = fistIndex; i < lastIndex; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
this.dlPaging.DataSource = dt;
this.dlPaging.DataBind();
this.dlBottomPaging.DataSource = dt;
this.dlBottomPaging.DataBind();
}
答案 0 :(得分:0)
如果您在页面加载时检查ISpostback,请从ISpostback scop中获取列表绑定。