Showing posts with label loadcontrol. Show all posts
Showing posts with label loadcontrol. Show all posts

Saturday, March 24, 2012

UpdatePanel and Dynamic Controls...

Hello all,

I have created a *.ascx (UserControl) which I added in the web form in UpdatePanel dynamically using Page.LoadControl function. The control is rendered fine. But the client side event handlers (that I defined in the javascript of the UserControl) are not rendered. When that event is raised, it raises the "Object Required" javascript exception. It happens because that function (handler) is not rendered. Please give me solution to this problem. Here is the sample

(this is done on server side Page_Load event handler)

WebCombo1.ClientSideEvents.BeforeDropDown =

"WebCombo_BeforeDropDown" + ControlID;

(Now I have defined the handler on the client side as)

function WebCombo_BeforeDropDown<%=ControlID%>(webComboId)

{

.... my code here...

}

Thanks in Advance

Mudassar

The reason it does not work is because innerHTML, which UpdatePanel framework uses on the client side, does not work for embedded script tags in HTML.

If you can register the script blocks using ScriptManager.RegisterClientScriptBlock that will be the best. So instaed of putting them in script Tags generate them in a string and use ScriptManager.RegisterClientScriptBlock.

UpdatePanel and Dynamically loaded controls (LoadControl)

Ok so this has been doing my head in for nearly 3 hours straight. Everytime I think I found a piece of code that could solve this problem, adapt it and try it... well I always get the same darn result.

Here's the structure
MasterPage
Page
TabContainer + TabPanel (AJAX)
UpdatePanel (in a tabpanel)
UserControl containing a second user control

I keep telling myself I may have gone crazy trying to work with such a structure, but i have to load a different part depending on the current page, but the base stays the same (so thats why I have imbricated usercontrols). I use LoadControl() at pageload to load the correct control.

Now on to the problem: The second user control (mainly consisting of a gridview, so I'll call it the grid control) disappears when I generate a postback from within itselft (like clicking on a link in the button column). If I generate a postback from the parent control, however, it works just fine. I tried a bunch of things, liek using the viewstate to reload the control, to no avail. I thought maybe it was my control structure that would be the problem, so I made a test user control with just a button called click. Same thing happens.

In debug, I realized that the gridcontrol's page_load event was never called when I generated a postback from itself. So i went radical and added an updatepanel around the page content. Seems to fix the issue, but I don't really want the events of that part of the page to be independant from the rest... Cuz some buttons on the parent user control are supposed to cause the data displayed in the gridcontrol to change. And unless I can set triggers from the parent control, which I don't really know how to do if it's possible, well it's not really useful.

I'd appreciate any input on this, I could post my code but I don't really think it'd be that useful, and I hope I've been clear anough.

Thanks for your time

Try registering programmatically the controls from your Grid Control to execute an async postback:

ScriptManager1.RegisterAsyncPostBackControl(control);