简体   繁体   中英

ASP.NET 2.0 postback stops working after first postback

I have a DataList control that is using a PagedDataSource, the page loads fine, data is displayed ok. I also have another DataList which provides page numbers for the paging.

When the user clicks on any of the page numbers if does a postback and increments the PagedDataSource CurrentPage property.

This works fine the first time you click it. The page reloads on the proper page. Clicking any page again however, does NOT cause a postback to occur. THe page seems to "flicker" But I get no server side codestop getting hit, unlike the first time I clicked it.

I'd assume the first postback is screwing up the postback somehow, but I don't know why.

Any ideas on this ? Here is the Page load and binding code..

    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();
}

如果在页面加载时检查ISpostback,请使列表绑定脱离ISpostback范围。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM