Problems and Solutions for embedding Javascript in updatepanel

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.