Asp.net calls JavaScript Functions in the frontend and backend

Source: Internet
Author: User

1. Call JavaScript Functions directly at the front-end

It's easy to add a script element between the head element and set the type element to "text/JavaScript"

For example:

<Head runat = "server">
<SCRIPT type = "text/JavaScript">
Function showname (STR)
{
Alert ("Your name is :(" + STR + ")");
}
</SCRIPT>
<Title> using JavaScript </title>
</Head>

Then, access JavaScript Functions through events between body elements. For example, Access JavaScript Functions through the onclientclick event of button1.

Example:

<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "showname ('xxx')"/>

When you click a button to run the project, "Your name is XXX" is displayed"

This is a simple JavaScript function.

2. Calling through JS files on the front-end

The method is the same as (1), but you only need to specify the. js file.

Example:

<Head runat = "server">
<SCRIPT type = "text/JavaScript" src = "jscript. js">
</SCRIPT>
<Title> using JavaScript </title>
</Head>

Then, access JavaScript Functions through events between body elements. For example, Access JavaScript Functions through the onclientclick event of button1.

Example:

// The showname method must be included in the. js file.

<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "showname ('xxx')"/>

3. Call JavaScript Functions in the background. The functions are in the. js file.

Head element at the front end

<Head runat = "server">
<SCRIPT type = "text/JavaScript" src = "jscript. js">
</SCRIPT>
<Title> using JavaScript </title>
</Head>

The following must be added to the background:Code

Button1.attributes. Add ("onclick", "showname1 (XXX )");

4. Call JavaScript Functions in the background. The functions are written in the. js file, but are not defined in the foreground.

// Obtain the. JS File

String myscript = "jscript. js ";

// Register the. js file. If you view the source code, you will get the following code:

// <SCRIPT> src = "jscript. js" type = "text/JavaScript"> <SCRIPT>

Page. clientscript. registerclientscriptinclude ("mykey", myscript );

// Same as above
Button1.attributes. Add ("onclick", "showname1 (123 )");

 

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.