Dynamically Created Gridview - Paging events are not fired

Discussion in 'ASP .Net' started by ami, May 9, 2007.

  1. ami

    ami Guest

    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.EnableViewSource = true;
    gv.PageIndexChanging += new
    GridViewPageEventHandler(gv_PageIndexChanging);


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

    And in my PageIndex event:


    private void gv_PageIndexChanging(object sender, GridViewPageEventArgs
    e)
    {


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

    gv.DataBind();

    }
    ami, May 9, 2007
    #1
    1. Advertising

  2. ami

    bruce barker Guest

    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.EnableViewSource = true;
    > gv.PageIndexChanging += new
    > GridViewPageEventHandler(gv_PageIndexChanging);
    >
    >
    > gv.Datasource = //bind it to my datasource
    > gv.Databind(); //databind
    >
    > And in my PageIndex event:
    >
    >
    > private void gv_PageIndexChanging(object sender, GridViewPageEventArgs
    > e)
    > {
    >
    >
    > // Set the CurrentPageIndex before binding the grid
    > Gridview gv = (GridView) sender;
    > gv.PageIndex = e.NewPageIndex;
    >
    > gv.DataBind();
    >
    > }
    >
    bruce barker, May 10, 2007
    #2
    1. Advertising

  3. 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 PageIndexChanged
    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
    PageIndexChanged 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.LoadControl() 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 PageIndexChanged 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.
    Valerio Santinelli, May 30, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tfsmag
    Replies:
    1
    Views:
    4,731
    tfsmag
    Mar 14, 2006
  2. Astrith
    Replies:
    0
    Views:
    349
    Astrith
    Sep 25, 2008
  3. msimmons
    Replies:
    0
    Views:
    458
    msimmons
    Jul 16, 2009
  4. Richard Huang
    Replies:
    1
    Views:
    124
  5. Replies:
    5
    Views:
    178
    Alessandro Zifiglio
    Aug 2, 2006
Loading...

Share This Page