|
| Javascript doesn't get loaded the right way when using dynamically loaded usercontrols |
 |
Mon, 25 Sep 2006 19:44:46 +000 |
Hi forum & developers,
I experience a problem regarding Javascript on Usercontrols when those are
loaded dynamically. By loading dynamically I mean the following:
UserControl myControl = LoadControl("MyControl.ascx");
Placeholder.Controls.Add(myControl);
If 'Placeholder' (let's say it's a <div> tag) is placed inside an
<atlas:UpdatePanel> and therefore the usercontrol is loaded via Ajax (and
not a complete roundtrip), every javascript contained on
the Usercontrol is ignored...
i.e.: If you have this on the usercontrol, it just throws a javascript error:
'object expected' when clicking the button:
<script type="text/javascript">
function SayHello()
{
alert('Hello');
}
</script>
<input type="button" value="Hello"
onclick="SayHello();" />
The problem is demonstrated in a simple application I created - you can download
it here:
http://www.softwaretailor.at/Files/AtlasTest.zip
Simply try to load the second Usercontrol and click the button - instead of
being greeted friendly you will get an unpolite javascript error!
There seems to be a workaround for this by using
'Page.ClientScript.RegisterStartupScript()' but as some third party controls
work with dynamically loaded Usercontrols (for example a grid which uses
Usercontrols as editforms), this workaround
does not work too good in most cases.
Hopefully i explained the topic indepth enough and someone (wave at microsoft
developers over there) will take care of this ;)
Kind regards from austria,
Wolfgang
|
| Post Reply
|
| Re: Javascript doesn't get loaded the right way when using dynamically loaded usercontrols |
 |
Mon, 25 Sep 2006 21:42:35 +000 |
hello.
i think that there isn't an easy solution to this problem (unless they parse the
html they receive on the client, which i'm not seeing). so, the solution to your
problem is to use the clientscriptmanager class and the registerXXX methods.
this will garantee that your code will allways be inserted correctly on the
page.
|
| Post Reply
|
| Re: Javascript doesn't get loaded the right way when using dynamically loaded usercontrols |
 |
Mon, 25 Sep 2006 23:18:14 +000 |
Exactly what Luis said.
Script that is inside UpdatePanels needs to be registered using
Page.ClientScript.RegisterXxx methods. In the final release of Atlas 1.0 we'll
have a new set of registration APIs to call, but they will be very similar to
the ones on Page.ClientScript.
Thanks,
Eilon
|
| Post Reply
|
| Re: Javascript doesn't get loaded the right way when using dynamically loaded usercontrols |
 |
Tue, 26 Sep 2006 16:05:55 +000 |
Hi there!
Sorry folks, but this is not a very acceptable method of "ajax
enabling" anything.... as I tried to explain, it will not be possible for
not a few third party controls to function correctly... Actually on such
Usercontrols there won't even work normal <asp:Validator> controls, will
they?
Regards,
Wolfgang
|
| Post Reply
|
| Re: Javascript doesn't get loaded the right way when using dynamically loaded usercontrols |
 |
Tue, 26 Sep 2006 16:23:48 +000 |
hello.
well, i haven't quite understood what you're saying about validators...regarding
the factof not beeing a very acceptable way of ajax enabling, well, in this case
it's really a problem related with the browser. let me explain. during a partial
postback, the server gets the html of the controls placed inside the updatepanel
(including the <script> tags) and packages it in an xml response which is
sent back to the client. in the client, the html is introduced in the page
through the use of the innerHTML property of the div/span that is rendered
initially by the updatepanel. the thing is that when you do obj.innerHTML =
"something" and you have a script block in somethign, that block
isn't interpreted as script (that's the way it works; scripts must be added to
a page in a different way). when you register a script using the registerXXX
api, those scripts are also inserted on the msg that is sent from the server;
however, the client portion of atlas (pagerequestmanager) interprets those
sections differently and do insert them in the page by using the DOM.
i guess that it's not impossible to do what you're saying. the problem is that
you'd have to parse the html you receive and this would affect the performance
of a partial postback.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|