Recently, I have encountered some questions about how to embed Javascript in updatepanel on the Microsoft Ajax forum.
The cause is that AJAX only replaces the innerhtml of the Div representing an updatepanel with the data returned from the server. these contents are not interpreted and executed by the javascript interpreter. none of the Javascript is available.
The javascript registered with scriptmanager is returned in a separate place, and Ajax will load them explicitly.
Can we manually load the embedded Javascript directly in the customer segment? After all, using embedded Javascript is a habit of many web developers.
Of course!
We need to dynamically create script elements andCodePut it in, so you can.
I will use the scriptloader. code example I wrote today as follows:
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default. aspx. CS " Inherits = " _ Default " %>
<! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en " " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head runat = " Server " >
< Title > Untitled page < / Title>
< Script SRC = " Scriptloader. js " Type = " Text/JavaScript " > < / SCRIPT>
< / Head>
< Body >
< Form ID = " Form1 " Runat = " Server " >
< Div >
< ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server " >
< / ASP: scriptmanager>
< Script Type = " Text/JavaScript " >
Function Endrequesthandler (sender, argS)
{
Window. scriptloader. loadscript ("Testscript","JScript. js",Null);
}
SYS. webforms. pagerequestmanager. getinstance (). add_endrequest (endrequesthandler );
< / SCRIPT>
< ASP: updatepanel ID = " Updatepanel1 " Runat = " Server " >
< Contenttemplate >
< ASP: Panel ID = " Panel1 " Runat = " Server " >
< Input ID = " Button2 " Type = " Button " Value = " Button " Onclick = " Return test () " / >
< / ASP: Panel>
< ASP: button ID = " Button1 " Runat = " Server " Text = " Button " Onclick = " Button#click " / >
< ASP: textbox ID = " Textbox1 " Runat = " Server " > < / ASP: textbox>
< / Contenttemplate>
< / ASP: updatepanel>
< / Div>
< / Form>
< / Body>
< / HTML>
Function Test ()
{
Alert ("Hello!");
}
If you click buton2 before you click button1, an error will occur. If you click button1 and then click buton2, no error will occur.