ASP. NET Web page dynamically add client Script

Source: Internet
Author: User

The System.Web.UI.Page class contains the Registerstarupscript () and RegisterClientScriptBlock () two methods, which can be used to dynamically add script blocks to a Web page. Client-side scripting can be divided into two categories: one is to run immediately after the page is loaded, and the other to run after a client event has occurred. A common example of the former is the pop-up of an ad bar as soon as the page is opened, a common example of which is a message box that pops up when the user clicks the Mo button.
the two methods of RegisterStartupScript () and RegisterClientScriptBlock () are as follows:

Page.registerstartupscript (string key,string  script); Page.registerclientscriptblock (string key,string script);

Parameter key: Is the unique identifier that generates the client script block;

Parameter script: is the client script block that will be generated, which is a string type.
The only difference between the two clock methods is that the location of the generated script is different;

(1) Registerstarupscript () method
Use the Registerstarupscript () method to add a script block that needs to run immediately after the page is loaded. The script block that is added by this method is located at the end of the Web form, before the label. For example, add the following code to the page Page_Load event:

Page.registerstartupscript ("key","<script>alert (' welcome you to this net dip! '); </script>"

To execute the above program, select the "Source Files" option from the "View" menu in IE browser, where the script block is generated as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"><HTML>    <HEAD>        <title>Web22</title>        <Metaname= "GENERATOR"Content= "Microsoft Visual Studio. NET 7.1">        <Metaname= "Code_language"Content= "C #">        <Metaname= "vs_defaultClientScript"content= "JavaScript">        <Metaname= "Vs_targetschema"content= "Http://schemas.microsoft.com/intellisense/ie5">    </HEAD>    <Bodyms_positioning= "GridLayout">        <formname= "Form1"Method= "POST"Action= "Web22.aspx"ID= "Form1"><inputtype= "hidden"name= "__viewstate"value= "ddwymta1nti4mte3ozs+rxjh8qxphipyqulsie2igmf+ksu=" />     <FONT Face= "Song Body">                <inputname= "TextBox1"type= "text"ID= "TextBox1"style= "Z-INDEX:101; left:464px; Position:absolute; top:48px " /></FONT>                <Script>Alert ('you are welcome to visit this website!');</Script></form>    </Body></HTML> 

(2) RegisterClientScriptBlock ()
The script block generated using the RegisterClientScriptBlock () method is located at the beginning of the Web page, which is "
Label. For example, add the following code to the page Page_Load event:

Page.registerclientscriptblock ("key","<script>alert (' Welcome to this website! '); </script>"

Compile the program, open the source file code for the page, and the script block generates the following location as shown below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"><HTML>    <HEAD>        <title>Web22</title>        <Metaname= "GENERATOR"Content= "Microsoft Visual Studio. NET 7.1">        <Metaname= "Code_language"Content= "C #">        <Metaname= "vs_defaultClientScript"content= "JavaScript">        <Metaname= "Vs_targetschema"content= "Http://schemas.microsoft.com/intellisense/ie5">    </HEAD>    <Bodyms_positioning= "GridLayout">        <formname= "Form1"Method= "POST"Action= "Web22.aspx"ID= "Form1"><inputtype= "hidden"name= "__viewstate"value= "ddwymta1nti4mte3ozs+rxjh8qxphipyqulsie2igmf+ksu=" />               <Script>Alert ('you are welcome to visit this website!');</Script>           <FONT Face= "Song Body">                <inputname= "TextBox1"type= "text"ID= "TextBox1"style= "Z-INDEX:101; left:464px; Position:absolute; top:48px " /></FONT>        </form>    </Body></HTML>

In addition, the page class provides auxiliary methods for both methods, which are isstartupscriptregistered (string key) and isclientscriptblockregistered (string key). Both methods accept a parameter key and return a Boolean value that is used to flag whether the script block with the keyword key has been added to the page. Returns true if the script block has been added to the page, otherwise false is returned. Use these two helper methods to avoid repeating the same script block on the same page.
Another way is to add a trigger condition to the control. If you add an "onclick" script event to a button control, you can use the "Button.Attributes.Add (" onclick ", function name)" Statement on the server.

this. BTNDEL.ATTRIBUTES.ADD ("onclick","return confirm (' Is it deleted? ')");

ASP. NET Web page dynamically add client Script

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.