Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. GridView Paging

GridView Paging

Scheduled Pinned Locked Moved ASP.NET
csssysadminalgorithmshelp
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fififlowertot
    wrote on last edited by
    #1

    Hi, I have a grid which is bound to data coming from a service at runtime. The issue is every time i select next page number the page goes blank. My code is as follows: for grid in aspx

    <div>
    <asp:Label ID="Label1" runat="server" Text="Searching....." Font-Bold = "true"></asp:Label>
    <asp:GridView ID="grid1" runat="server" OnPageIndexChanging= "grid1_PageIndexChanged"
    AutoGenerateColumns="False" Enabled ="false" AllowPaging="True" PageSize="5">
    <Columns>
    <asp:BoundField DataField = "Name"/>
    <asp:BoundField DataField = "ID"/>
    </Columns></asp:GridView>

            </div>
    

    the codebehind(.cs) is typically is as follows:

    on pageload:

    grid1.Enabled = true;
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("ID");
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    dt.Rows.Add("Jack", 1);
    dt.Rows.Add("Jill", 2);
    grid1.DataSource = dt;
    grid1.DataBind();

    protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e)
    {

        grid1.PageIndex = e.NewPageIndex;
        grid1.DataBind();
    }
    

    what am i missing here

    R L S 3 Replies Last reply
    0
    • F fififlowertot

      Hi, I have a grid which is bound to data coming from a service at runtime. The issue is every time i select next page number the page goes blank. My code is as follows: for grid in aspx

      <div>
      <asp:Label ID="Label1" runat="server" Text="Searching....." Font-Bold = "true"></asp:Label>
      <asp:GridView ID="grid1" runat="server" OnPageIndexChanging= "grid1_PageIndexChanged"
      AutoGenerateColumns="False" Enabled ="false" AllowPaging="True" PageSize="5">
      <Columns>
      <asp:BoundField DataField = "Name"/>
      <asp:BoundField DataField = "ID"/>
      </Columns></asp:GridView>

              </div>
      

      the codebehind(.cs) is typically is as follows:

      on pageload:

      grid1.Enabled = true;
      DataTable dt = new DataTable();
      dt.Columns.Add("Name");
      dt.Columns.Add("ID");
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      dt.Rows.Add("Jack", 1);
      dt.Rows.Add("Jill", 2);
      grid1.DataSource = dt;
      grid1.DataBind();

      protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e)
      {

          grid1.PageIndex = e.NewPageIndex;
          grid1.DataBind();
      }
      

      what am i missing here

      R Offline
      R Offline
      Ravi Sant
      wrote on last edited by
      #2

      I think you need to include your Page_Load code inside If( ! PostBack ) { } block. Please try this.

      ♫ 99 little bugs in the code, 99 bugs in the code We fix a bug, compile it again 101 little bugs in the code ♫

      F 1 Reply Last reply
      0
      • R Ravi Sant

        I think you need to include your Page_Load code inside If( ! PostBack ) { } block. Please try this.

        ♫ 99 little bugs in the code, 99 bugs in the code We fix a bug, compile it again 101 little bugs in the code ♫

        F Offline
        F Offline
        fififlowertot
        wrote on last edited by
        #3

        yes my code is inside if (!IsPostBack) block.. but evrytime i click say page 2 the screen goes blank.

        1 Reply Last reply
        0
        • F fififlowertot

          Hi, I have a grid which is bound to data coming from a service at runtime. The issue is every time i select next page number the page goes blank. My code is as follows: for grid in aspx

          <div>
          <asp:Label ID="Label1" runat="server" Text="Searching....." Font-Bold = "true"></asp:Label>
          <asp:GridView ID="grid1" runat="server" OnPageIndexChanging= "grid1_PageIndexChanged"
          AutoGenerateColumns="False" Enabled ="false" AllowPaging="True" PageSize="5">
          <Columns>
          <asp:BoundField DataField = "Name"/>
          <asp:BoundField DataField = "ID"/>
          </Columns></asp:GridView>

                  </div>
          

          the codebehind(.cs) is typically is as follows:

          on pageload:

          grid1.Enabled = true;
          DataTable dt = new DataTable();
          dt.Columns.Add("Name");
          dt.Columns.Add("ID");
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          dt.Rows.Add("Jack", 1);
          dt.Rows.Add("Jill", 2);
          grid1.DataSource = dt;
          grid1.DataBind();

          protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e)
          {

              grid1.PageIndex = e.NewPageIndex;
              grid1.DataBind();
          }
          

          what am i missing here

          L Offline
          L Offline
          Legor
          wrote on last edited by
          #4

          I can't reproduce this behaviour. When paging through the Grid the screen doesn't go blank. Im using VS 2010. Maybe it has something to do with the data source you've bound to the control?

          F 1 Reply Last reply
          0
          • L Legor

            I can't reproduce this behaviour. When paging through the Grid the screen doesn't go blank. Im using VS 2010. Maybe it has something to do with the data source you've bound to the control?

            F Offline
            F Offline
            fififlowertot
            wrote on last edited by
            #5

            my datasource is a list of objects returned from the service. Since I need to format it before displaying I convert it into a datatable and this datatable is datasource to the grid. I have stepped thru and found that datatable has populated fine with all the values. even the page comes up fine with all the data and 123 page links down. it's when i click page 2/3 link in pager that it goes blank. am using vs 2008 .net 3.5

            V 1 Reply Last reply
            0
            • F fififlowertot

              my datasource is a list of objects returned from the service. Since I need to format it before displaying I convert it into a datatable and this datatable is datasource to the grid. I have stepped thru and found that datatable has populated fine with all the values. even the page comes up fine with all the data and 123 page links down. it's when i click page 2/3 link in pager that it goes blank. am using vs 2008 .net 3.5

              V Offline
              V Offline
              vanikanc
              wrote on last edited by
              #6

              You should probably separate all the three subroutines. Have the !(PagePostback), datagrid itself, and the Grid1_PageIndexChanging all separate and not in one sub.

              F 1 Reply Last reply
              0
              • V vanikanc

                You should probably separate all the three subroutines. Have the !(PagePostback), datagrid itself, and the Grid1_PageIndexChanging all separate and not in one sub.

                F Offline
                F Offline
                fififlowertot
                wrote on last edited by
                #7

                my page load checks if it's not postback it calls the gridview load which does what i've posted and i have seperate event handler.

                V A 2 Replies Last reply
                0
                • F fififlowertot

                  my page load checks if it's not postback it calls the gridview load which does what i've posted and i have seperate event handler.

                  V Offline
                  V Offline
                  vanikanc
                  wrote on last edited by
                  #8

                  I had the same issue. I had to literally re-bind the entire datagrid for it to work. It worked for me, and was working under time cruch, I stuck with it.

                  F 1 Reply Last reply
                  0
                  • V vanikanc

                    I had the same issue. I had to literally re-bind the entire datagrid for it to work. It worked for me, and was working under time cruch, I stuck with it.

                    F Offline
                    F Offline
                    fififlowertot
                    wrote on last edited by
                    #9

                    what do u mean by rebind, rewrite the code?

                    1 Reply Last reply
                    0
                    • F fififlowertot

                      Hi, I have a grid which is bound to data coming from a service at runtime. The issue is every time i select next page number the page goes blank. My code is as follows: for grid in aspx

                      <div>
                      <asp:Label ID="Label1" runat="server" Text="Searching....." Font-Bold = "true"></asp:Label>
                      <asp:GridView ID="grid1" runat="server" OnPageIndexChanging= "grid1_PageIndexChanged"
                      AutoGenerateColumns="False" Enabled ="false" AllowPaging="True" PageSize="5">
                      <Columns>
                      <asp:BoundField DataField = "Name"/>
                      <asp:BoundField DataField = "ID"/>
                      </Columns></asp:GridView>

                              </div>
                      

                      the codebehind(.cs) is typically is as follows:

                      on pageload:

                      grid1.Enabled = true;
                      DataTable dt = new DataTable();
                      dt.Columns.Add("Name");
                      dt.Columns.Add("ID");
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      dt.Rows.Add("Jack", 1);
                      dt.Rows.Add("Jill", 2);
                      grid1.DataSource = dt;
                      grid1.DataBind();

                      protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e)
                      {

                          grid1.PageIndex = e.NewPageIndex;
                          grid1.DataBind();
                      }
                      

                      what am i missing here

                      S Offline
                      S Offline
                      Sneha Bisht
                      wrote on last edited by
                      #10

                      copy this aspx.cs page using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BIndgrid(); } } void BIndgrid() { grid1.Enabled = true; DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("ID"); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); grid1.DataSource = dt; grid1.DataBind(); } protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e) { grid1.PageIndex = e.NewPageIndex; grid1.DataBind(); BIndgrid(); } }

                      F 1 Reply Last reply
                      0
                      • S Sneha Bisht

                        copy this aspx.cs page using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BIndgrid(); } } void BIndgrid() { grid1.Enabled = true; DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("ID"); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); dt.Rows.Add("Jack", 1); dt.Rows.Add("Jill", 2); grid1.DataSource = dt; grid1.DataBind(); } protected void grid1_PageIndexChanged(Object sender, GridViewPageEventArgs e) { grid1.PageIndex = e.NewPageIndex; grid1.DataBind(); BIndgrid(); } }

                        F Offline
                        F Offline
                        fififlowertot
                        wrote on last edited by
                        #11

                        Super. Thanks Sneha Bisht

                        1 Reply Last reply
                        0
                        • F fififlowertot

                          my page load checks if it's not postback it calls the gridview load which does what i've posted and i have seperate event handler.

                          A Offline
                          A Offline
                          Anky2011
                          wrote on last edited by
                          #12

                          Hi , Your Page_Load checks if it is not a postback right ? This means your code sets the DataSource property of your grid ONLY once and that is when the page loads for the first time. Please write the code "grid.DataSource = dataTable;" just before the code "grid.DataBind()" and let me know if it works. Always make it a habit to set the DataSource of a grid just before calling the DataBind() method on the grid. E.g. I would always code method like "BindPersonsData()" in my application which would be like - private void BindPersonsData() { grdPersons.DataSource = GetPersons(); grdPersons.DataBind(); } private DataTable GetPersons() { DataTable dt = new DataTable(); //logic to get data return dt; }

                          ------------- Ankur - The Tech Anky Think Simple, Code Simple, Do Big

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups