5

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?

ARr0w
  • 1,701
  • 15
  • 31

3 Answers3

2
  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.

Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
0

Save your DataSet somewhere like ViewState on LinkButton_Panel_ViewStock_Click after filling DataSet like this

ViewState["ds"] = dtStockView

In PageIndexChanging write like this

protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    panel_ViewStock.visible = true;
    GridView_Stock.PageIndex = e.NewPageIndex;
    GridView_Stock.DataSource = ViewState["ds"] as DataSet
    GridView_Stock.DataBind();
}

Hope this will help you

Sharique Ansari
  • 1,458
  • 1
  • 12
  • 22
  • It Didn't work bro, same issue. The scenario i have in my mind is that on clicking on the pagechanging event it reloads the page but do not opens this viewstock panel. there are 3 panels. (1) Add Stock (2) Update Stock (3) View Stock and this is what i have on page load on page load " if(!ispostback){ panel_updatestock.visible = false; panel_ViewStock.visible = false; } – ARr0w Jun 05 '16 at 13:06
  • I tried the modified answer as well bro, but it didn't work. But still thanks for the time and help <3. Appreciated ^-^. cos Denis Wessels logic worked! :) – ARr0w Jun 07 '16 at 07:36
0

You can try to use:

protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
Draken
  • 3,134
  • 13
  • 34
  • 54