[MasterPages practical skills] Added front-end control for templates 2 - Automatic registration client global script

zhaozj2021-02-16  102

Let us first understand the meaning of the "global".

The HTML page can define a client script that provides rich visual behavior and logic for the appearance of the page, and the client script can be written in or . The script written in is the "global" script referred to in the text, why is it global? Because the client script defined in can be accessed anywhere in , and the client script defined outside can also be loaded after the page is loaded, in order The browser is executed, and all objects in the page can be accessed. The script in can only be executed in order, where the definition can only be called by the following client script, and the page object that it accesses must also be defined before it.

Since MASTERPAGES cannot correctly output the Region / Content defined in , then provide client global script registration tasks for each form, and also fall on the Page_Load event handler of the template control.

First, define in the template control (the control code header registration tag section, please refer to previous):

... ...

Then, add code to the template control: if (regscript.controls.count> 0) {system.web.ui.literalcontrol script = (System.Web.ui.literalcontrol) Regscript.controls [0]. Controls [0]; // MasterPages content to the content will be placed in a LiteralControl Panel, and then into this Panel region this.Page.RegisterClientScriptBlock ( "client_script", script.Text);} RegisterClientScriptBlock Page class is One way, the role is to register the client global script for the form, it has two parameters: the first parameter specifies the registered script segment name, if there is a script segment in the form, the existing script will be replaced. The second parameter is the script content that is registered, must be a full script containing the