Dynamically Created Gridview - Paging events are not fired

A

ami

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();

}
 
B

bruce barker

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

-- bruce (sqlwork.com)
 
V

Valerio Santinelli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top