Differences between two methods for dynamically adding client scripts to ASP. NET web pages

Source: Internet
Author: User

The page class provides a clientscript attribute to manage scripts registered in the background, which is most often used to register scripts at the foreground. I think there are two obfuscation-prone Methods: registerclientscriptblock and registerstartupscript. I checked the difference between the two methods carefully and made a record.

Registerclientscriptblock is the front of all HTML elements in form, and the script block is loaded after the viewstate element. If you use HTML elements in the script, you use document. for operations such as getelementbyid, a script error is reported and an object is missing. Because the elements used in your script have not been loaded while the script is running, it is normal to lack objects. Msdn explains: Add a script block at the top of the rendered page.

Registerstartupscript loads script blocks after all HTML elements before </form>. At this time, all HTML elements have been loaded, and document is used. operations such as getelementbyid can be executed normally. In msdn, the added script block is executed before the page load is completed but the onload event of the page is triggered.

The two methods can be used flexibly as needed. If a simple dialog box is displayed, you can use the former. If you need to use HTML elements on the page, use the latter.

Maybe the explanation in msdn is more professional, but I think my understanding is more straightforward.

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.