Interaction between the js front-end and the background data-call the front-end at the background (call and register the client script at the background)

Source: Internet
Author: User
Client scripts are generally on the frontend. Here we talk about (1) Calling the scripts defined by the frontend in the background (2) how to register the client script usage in the background when to use the server code to add the client script to the page: u the client script content is available only when it is running

Client scripts are generally on the frontend. Here we will talk about (1) Calling the scripts defined by the frontend in the background (2) how to register client scripts in the background


Purpose

When to add a client script to the page using the server code:

U when the content of the client script is dependent on the information that is available until running

U when you want the client script to be executed when the page has been loaded or when the user submits the page


Method
(1) Write a script using the Response. Write Method

For example, after you click the button, you must first operate the database. After the operation is complete, a dialog box is displayed, prompting Response. Write (" Alert (); script ");

Defect: This method cannot call the User-Defined Functions in the script file. It can only call internal functions. Specifically, the user-defined functions can only be defined in Response. Write, such as Response. Write (" Function myfun () {alert ('A')} script ");

(Note that the next response method is different from the previous one and does not pop up immediately because it only registers a client Script Function and does not execute this function. Therefore, only myfun () is called () function)


(2) Obtain references to the ClientScriptManager class through the ClientScript attribute of the Page object to dynamically register and call client scripts

ClientScriptManager class Introduction: The ClientScriptManager class is used to manage client scripts and add them to Web applications. The scripts are uniquely identified by the String and Type keys. Scripts with the same key and type are considered as repeated scripts. Using script types helps avoid obfuscation of similar scripts that may be used in pages from different user controls.


(1) method ClientScriptManager. RegisterStartupScript (Type type, String key, String script)

  Parameters

Type: type of the startup script to be registered (this. GetType () is generally entered directly ).

Key: the key of the startup script to be registered (equivalent to a name for the script execution, any name ).

Script: the startup script Text to be registered ("script" function () script ", function () can be either a system function or a js function defined on the foreground ).


? The client script consists of its key and type (Type) Unique identifier. Scripts with the same key and type are considered as repeated scripts.

? Call the IsStartupScriptRegistered method to check whether the startup script with the given key and type pair has been registered. This avoids unnecessary script addition attempts.

? The script block added by the RegisterStartupScript method is executed before the page is loaded but the OnLoad event of the page is triggered.


Example:

  Front-end code:

  

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "RegisterStartupScript. aspx. cs" Inherits = "WebApplication4.WebForm12" %>
         
Related Article

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.