• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1892
  • Last Modified:

GridView Paging Postback Issue

Hello there,

I have a Gridview which is set to page. The problem is that I also have a button which executes a function. This function will only work if the databinding is in a if statement for !postback.

Whenever I try to page this GridView it gives me a blank page. I know why this is but is there a work around for it?

Thanks
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = ImageList.GetImages();
            GridView1.DataBind();
        }
    }
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

Open in new window

0
thomasmutton
Asked:
thomasmutton
2 Solutions
 
Richard LeeSoftware EnthusiastCommented:
Try the following:

Considering that the data is being retrieved every time you may want to consider caching or manual paging.
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = ImageList.GetImages();
            GridView1.DataBind();
        }
    }
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ImageList.GetImages();
        GridView1.DataBind();
    }
}

Open in new window

0
 
sunithnairCommented:
Try this
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = ImageList.GetImages();
            GridView1.DataBind();
        }
    }
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ImageList.GetImages();
        GridView1.DataBind();
    }

Open in new window

0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now