Dynamically Created Gridview - Paging events are not fired

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ami

    Dynamically Created Gridview - Paging events are not fired

    In my code, I need to implement x numbers of Gridviews dynamically
    based on user selection. Because I am unaware of the number of
    gridviews, this is done after the user enters input.

    I need to implement paging on these gridviews. I set paging to true
    and register the event handler. However, this event handler is never
    being fired (this has been tested by using the VS2005 debugger). My
    first page of results shows up fine. When a page button is clicked
    (to go to page 2, for example), the table disappears. Any help would
    be appreciated.

    Here are some snippets of my code (from my code behind page):


    Gridview gv = new Gridview();
    gv.AllowPaging = true;
    gv.EnableViewSo urce = true;
    gv.PageIndexCha nging += new
    GridViewPageEve ntHandler(gv_Pa geIndexChanging );


    gv.Datasource = //bind it to my datasource
    gv.Databind(); //databind

    And in my PageIndex event:


    private void gv_PageIndexCha nging(object sender, GridViewPageEve ntArgs
    e)
    {


    // Set the CurrentPageInde x before binding the grid
    Gridview gv = (GridView) sender;
    gv.PageIndex = e.NewPageIndex;

    gv.DataBind();

    }

  • bruce barker

    #2
    Re: Dynamically Created Gridview - Paging events are not fired

    you need to re-add the grids and event hooksups in the oninit of the
    next postback.

    -- bruce (sqlwork.com)

    ami wrote:
    In my code, I need to implement x numbers of Gridviews dynamically
    based on user selection. Because I am unaware of the number of
    gridviews, this is done after the user enters input.
    >
    I need to implement paging on these gridviews. I set paging to true
    and register the event handler. However, this event handler is never
    being fired (this has been tested by using the VS2005 debugger). My
    first page of results shows up fine. When a page button is clicked
    (to go to page 2, for example), the table disappears. Any help would
    be appreciated.
    >
    Here are some snippets of my code (from my code behind page):
    >
    >
    Gridview gv = new Gridview();
    gv.AllowPaging = true;
    gv.EnableViewSo urce = true;
    gv.PageIndexCha nging += new
    GridViewPageEve ntHandler(gv_Pa geIndexChanging );
    >
    >
    gv.Datasource = //bind it to my datasource
    gv.Databind(); //databind
    >
    And in my PageIndex event:
    >
    >
    private void gv_PageIndexCha nging(object sender, GridViewPageEve ntArgs
    e)
    {
    >
    >
    // Set the CurrentPageInde x before binding the grid
    Gridview gv = (GridView) sender;
    gv.PageIndex = e.NewPageIndex;
    >
    gv.DataBind();
    >
    }
    >

    Comment

    • Valerio Santinelli

      #3
      Re: Dynamically Created Gridview - Paging events are not fired

      I'm having a similar problem but in my case the grid does not disappear
      (as I do all the initialization on Page_Init()) but it just reloads the
      page and nothing changes.

      If I trace through the code I can clearly see that the PageIndexChange d
      event is never being fired. Which is weird considering that doing the
      same with, for example, the Click event of a button it works fine.

      Another important detail is that this happens only when loading the
      control dynamically.

      In my case I have a UserControl that contains a GridView. The GridView
      is being initialized during the Init event of the UserControl and the
      PageIndexChange d event is being hooked to a function in that control's
      code.

      Then I create the control on my aspx page by using the
      Page.LoadContro l() method and assign the result to the Controls
      collection of a placeholder that I placed on that page.

      The result is that if I do like that, events like PageIndexChange d do
      not fire.

      If, instead, I drag & drop the user control that I've created into the
      page and then make it run, it works fine.

      Has anyone got any idea about the reason of this behavior?

      Thanks!

      Valerio

      bruce barker wrote:
      you need to re-add the grids and event hooksups in the oninit of the
      next postback.
      >
      -- bruce (sqlwork.com)
      >
      ami wrote:
      >In my code, I need to implement x numbers of Gridviews dynamically
      >based on user selection. Because I am unaware of the number of
      >gridviews, this is done after the user enters input.
      >>
      >I need to implement paging on these gridviews. I set paging to true
      >and register the event handler. However, this event handler is never
      >being fired (this has been tested by using the VS2005 debugger). My
      >first page of results shows up fine. When a page button is clicked
      >(to go to page 2, for example), the table disappears. Any help would
      >be appreciated.

      Comment

      Working...