Javascript learning notes (III)-integrate Javascript into ASP. NET web pages

Source: Internet
Author: User

Javascript is the mainstream programming language for Web application clients. In actual projects, you often need to dynamically add JavaScript code to ASP. NET webpages.

1. Add JavaScript code directly in the control declaration

<Asp: button id = "button2" runat = "server" onmouseover = "this. style. color = 'red' "onmouseout =" this. style. color = 'black' "text =" color-changing buttons "/>

Onmouseover and onmouseout are not the attributes of the button control, but the event attributes of the HTML element. When an HTML page is displayed, ASP. NET will uncertain send the attributes that cannot be parsed to the browser client.

2. Use the control. Attributes. Add method to add JavaScript code

<SCRIPT type = "text/JavaScript">
// Implement the JavaScript function of the character counter
Function inputcounter (inputid, counterid, maxlength)
{
VaR TXT = Document. getelementbyid (inputid );
VaR counterid = Document. getelementbyid (counterid );
If (txt! = NULL) & (counterid! = NULL ))
{
Counterid. value = (parseint (maxlength)-TXT. value. Length). tostring ();
}
}
</SCRIPT>

Protected void page_load (Object sender, eventargs E)
{
// Dynamically "Assemble" JavaScript scripts,
// Inputcounter () is a JavaScript function defined in the String script = "inputcounter ('{0}', '{1}', {2 });";
Script = string. Format (script, txtinput. clientid, txtcount. clientid, 2000 );
// Append the Javascript script to the properties of the text box control.
Txtinput. Attributes. Add ("onkeyup", script );
}

3. Use registerstartupscript and other methods

The page class provides a clientscript object, which has a series of methods to dynamically inject Javascript code into ASP. NET pages.

<Input id = "htmlbutton1" type = "button" runat = "server" value = "I am a common HTML button, and now there is no response code"/>
<HR/>
<Asp: button id = "btnregisterjavascript" runat = "server" text = "dynamic JavaScript code injection to webpages"
Onclick = "btnregisterjavascript_click"/>

Protected void btnregisterjavascript_click (Object sender, eventargs E)
{
// JavaScript code block to be added
String script = "function sayhello () {alert (\" Hello \");}";

// Any of the following two sentences can add JavaScript code blocks to the page
// Identify the differences between the two methods through the generated HTML webpage source code
// Clientscript. registerclientscriptblock (this. GetType (), "sayhello", script, true );
Clientscript. registerstartupscript (this. GetType (), "sayhello", script, true );

Htmlbutton1.value = "I am a common HTML button, and now the response code is dynamically mounted ";
// Add an event response function to the button
Htmlbutton1.attributes ["onclick"] = "sayhello ();";
}

3.1 differences between registerclientscriptblock and registerstartupscrip

The two methods have different code positions in the client: the JavaScript code added by registerclientscriptblock appears inside the <body> element of the page, which is often mixed with other HTML code, therefore, these codes may not be able to access all HTML elements on the page. The javascript code added by registerstartupscript appears at the end of the <body> element.

This article mainly references Mr. Jin xuliang's ASP. NET Programming Tutorial.

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.