When adding user control (with Telerik RadGrid instance in it) at runtime into a panel (calling the LoadControl method), the events for this user control may not be fired on postback if you do not take into account the instructions below.
A requirement when you load user controls dynamically on your page, is to load the user control(s) on PageLoad of the main page. That is the reason the designers of ASP.NET made the PageLoad event execute before postback events - to give you a chance to reload controls so their events will fire.
The problem with later loading of user control (for example inside a server event handler of control on the page) is that the browser is sending your page events for controls that don't exist (you added them at runtime the last time the code ran, but at a later stage they're gone), so obviously their events won't be fired.
In a nutshell - what you need to do is recreate (in the page load event) the state of the page to exactly how it was the last time you output it to the browser. This way the user controls exist and ASP.NET will fire their events appropriately. This involves reloading the user control you loaded last time. Make sure it gets the same ID, name, etc.
Additional information about how to load user controls dynamically you can find in this documentation topic.
 |
If you load the user control (wrapping the grid) dynamically each time without conditional check, you should do that on PageInit instead of PageLoad:
protected void Page_Init(object sender, EventArgs e) { this.Panel1.Controls.Clear(); Control myControl = this.LoadControl("gridUC.ascx"); myControl.ID = "myUC"; this.Panel1.Controls.Add(myControl); }
Otherwise, you have to load the user control only the first time:
protected void Page_Load(object sender, EventArgs e) { if (Page.FindControl("myUC") == null) { Control myControl = this.LoadControl("gridUC.ascx"); myControl.ID = "myUC"; this.Panel1.Controls.Add(myControl); } }
|