. Net outputs js to page and updatepanel

Source: Internet
Author: User


We all know that there are many ways to output content to the client, and the beginner will use respone. Write (string ). For example:

Respone. Write ("Hello word !");

Or output JS

Respone. Write ("<SCRIPT type = 'text/JavaScript '> alert ('Hello word! '); </SCRIPT> ");

However, when you view the client source code, you will find that the output content is displayed at the front end of the source code. Obviously, it destroys the HTML format, in some cases, this will affect the page layout and other effects.

The correct output method should be: This. clientscript. registerstartupscript or this. clientscript. registerclientscriptblock.

This. clientscript. registerstartupscript is the first line of registration script at the beginning of form. The latter is the registration script at the end of form. In this way, the HTML format will not be damaged, for example:

This. clientscript. registerstartupscript (this. GetType (), "scriptkey", "<SCRIPT type = 'text/JavaScript '> alert ('Hello word! '); </SCRIPT> ")


This. clientscript. registerstartupscript (this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)

This. clientscript. registerclientscriptblock is similar.


When you want to output a piece of js in updatepanel, the above method will not achieve the expected results. Take a look at the example.

There is an updatepanel ID that is uppn

Scriptmanager. registerclientscriptblock (uppn, this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)


Scriptmanager. registerstartupscript (uppn, this. GetType (), "scriptkey", "alert ('Hello word! '); ", True)

In this case, when the updatepanel content is loaded to the client, "Hello word!" will pop up !" Dialog box.

In this way, it is more convenient to output JS from the background.

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.