Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView don't Show on GridView_PageIndexChanging

I have a panel ViewStock where i am viewing stock in a gridview from database and DataBind() it via code. Allowed paging and created and event "OnPageIndexChanging" in gridview tag in html, Implemented the defined code above and paging in an event as follows:

HTML:

<asp:Panel ID="Panel_StockView" runat="server">

    <asp:GridView ID="GridView_Stock" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" runat="server"></asp:GridView>

</asp:Panel>

Code C#:

protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(cs))
    {
        //Sql command here
        /sql adapter and filled datatable
        sdaStockView.Fill(dtStockView);
        GridView_Stock.DataSource = dtStockView;
        GridView_Stock.DataBind();
    }
}

And now the Implemented Paging

protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView_Stock.DataBind();
    GridView_Stock.PageIndex = e.NewPageIndex;
}

it does work but partially. It does the paging and does the data correctly. But, the issue is when i click the page '2' the panel blanks out just like in the picture i uploaded See this Image, then i click the link button that redirects me to the panel again and opens the page '2' of the gridview with valid data.

How to resolve this issue?

like image 584
ARr0w Avatar asked Oct 30 '22 02:10

ARr0w


1 Answers

  1. Extract the logic which binds the GridView to data into a new method.You can call it BindData() for example:

    private void BindData()
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            sdastockview.fill(dtstockview);
            gridview_stock.datasource = dtstockview;
            gridview_stock.databind();
        }
    }
    
  2. Call this method inside LinkButton_Panel_ViewStock_Click to populate the GridView:

    protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    
  3. Lastly, call it again to re-populate the GridView during paging:

    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_Stock.PageIndex = e.NewPageIndex;
        this.BindData();
    }
    

Just make those three little changes and it will work. I've tried this on my side and it's working just fine.

like image 186
Denys Wessels Avatar answered Nov 09 '22 09:11

Denys Wessels