Dynamically add client scripts to ASP. NET webpages

Source: Internet
Author: User
Add example JS and other scripts to the server in vs2005
This. Page. clientscript. registerstartupscript (type, string key, string value)
The usage is as follows:
Clientscript. registerclientscriptblock ( This. GetType (), "Test", "<script language = 'javascript '> alert ('test'); </SCRIPT> ");

The registerstartupscript method issues this script before the end mark of the <form runat = Server> element of the page object. Be sure to include the start and end <SCRIPT> elements around the script block string specified by the script parameter.

Because this method uses keys to identify the script block, you do not need to issue the script block to the output stream every time different Server Control instances request the script block.

Any script block with the same key value is considered repeated.

  • On the serverCode.

    Method Description

    Add a script block to the top of the page. Create a script as a string and pass it to the method. The method then adds the script to the page. You can use this method to insert any script into the page. Note that the script may be rendered to the page before all elements are completed. Therefore, you may not be able to reference all elements on the page from the script.

    Similar to the registerclientscriptblock method, this method adds a script block that references an external. js file. Include files added before any other dynamically added scripts; therefore, you may not be able to reference certain elements on the page.

    Add a script block to the page, which is executed before the onload event of the page is triggered after the page is loaded. This script is generally not created for event processing.ProgramOr function; it usually only contains the statement to be executed once.

    The script executed by adding the onsubmit event on the response page. This script is executed before the submission page. You can cancel the submission.

    The following code example shows how to add a client script to the page that is executed when the user clicks the button (this button returns the page to the server. The client script is displayed in the pop-up window, requesting the user to confirm sending back.

Protected void page_load (Object sender, eventargs e) {string scripttext = "Return confirm ('Do you want to submit the page? ') "; Clientscript. registeronsubmitstatement (this. GetType ()," confirmsubmit ", scripttext );}

You can add client scripts to controls on ASP. NET web pages as declared for HTML elements. Alternatively, if the event or code dependency is only available at runtime, you can add client script events to the ASP. NET web server control programmatically.

Note:

You can reference the control by ID in the client script. For more information, see client scripts on ASP. NET web pages.

Adding a client script onclick event to the button requires a special process, which will be described later in this topic.

Note:

The support for client scripts depends on the browser. For example, Internet Explorer, Mozilla, and mobile browsers may provide different types of support for client scripts.

Add client event handlers to ASP. NET Server controls in declarative form
    • Add the event property in the tag of the control, for example, onmouseover or onkeyup. Add the client script to be executed for the property value.

      Note:

      Always add a semicolon (;) after the client script in the property (;). This is required. In this way, you can run this code first when ASP. NET generates a client script for the control (for example, if the autopostback attribute of the control is set to true.

      Any attribute/value pairs in the control tag that do not correspond to the control property are transmitted to the browser as they are.

      The following code example shows an ASP. NET webpage that contains a client script. When you move the cursor over a button, the script changes the text color of the button.

C #
 <% @ page Language =" C # "%>    untitled page     
      
       
        
Add client event handlers to ASP. NET controls programmatically
    • In Or The Set Method.

      The following code example shows how Control to add client scripts. The client script displays the text length in the Textbox Control. This script assumes that the page contains a span element named spancounter.

C #
Protected void page_load (Object sender, eventargs e) {string displaycontrolname = "spancounter"; textbox1.attributes. add ("onkeyup", displaycontrolname + ". innertext = This. value. length ;");}
Add the client onclick event to the button control
    • In the button control ( , And Control), set the onclientclick attribute to the client script to be executed.

      The following code example shows how to add a client script click event to the button control.

C #
 <% @ page Language =" C # "%>    
      
       
      

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.