JavaScript and ASP. NET controls Integration

Source: Internet
Author: User

Although ASP. NET provides a strong platform, developers should not ignore mature technologies such as JavaScript. In this article, Tony Patton explains how to integrate JavaScript with ASP. NET controls in Web development.

Although the Web development platform provides flexibility and many functions, you often want to or need to rely on existing technologies to complete a required task. A good example is ASP. NET, which provides a powerful development platform, but it should not ignore mature technologies such as JavaScript. In this article, I will explain how to link JavaScript code to ASP.. NET Control.

Normal Mode

Generally, when a Web application requires a pop-up window, confirmation window, or other client functions, you can create necessary JavaScript methods and call them as needed. For example, if List A contains A confirmation window, you can use it to continue or cancel form submission.

After the submit button is selected, the confirmSubmit method of JavaScript is called. This confirmation prompt allows users to continue form submission (select confirmation) or cancel (select cancellation ).

The above code can work as planned, but it is not that easy to work on a development platform, such as ASP. NET.

ASP. NET replacement options

ASP. NET supports the use of HTML elements such as input buttons and text boxes. However, there is a set of complete control elements in the ASP. NET development model to provide additional functions.

Append JavaScript to ASP. NET is not as simple as the direct HTML/JavaScript method, ASP. NET programming mode provides methods in the basic page class to attach the script block to the page element. The following methods can be used to append or integrate JavaScript into pages or page elements:

RegisterClientScriptBlock: allows your page to contain script blocks. client scripts run after the starting tag of the form runat = server> element of the page object. The script block is submitted and output as an object, therefore, you must add two script> element tags.

RegisterOnSubmitStatement: allows you to assign a script block/method to the OnSubmit event of the page object.

RegisterStartupScript: allows you to include a script block in the page. Similar to the RegisterClientScriptBlock method, this method starts running before the end tag of the form runat = server> element of the page object, the script block is submitted and output as an object. Therefore, you must add two script> element labels.

Each method can accept two parameters: Key and script. The key is the name assigned to the script block, and the key value should be unique, instances controlled by multiple servers can request the script block, without having to run the output stream again. The second parameter script contains the actual script sent to the customer, it can be a complete JavaScript code or a method name.

These methods can be used in the actual ASP. NET page code, whether it is VB. NET, C #, J #, or any other language. List B shows ASP. NET sample code.

The JavaScript method is constructed using a string variable. Its value is passed to the RegisterClientScriptBlock method as the second parameter, the actual method name is allocated to ASP through the Add method of the Attributes attribute of the object. the JavaScript event is passed as the first parameter, while the method name is the second parameter.

In addition, you may notice the use of the IsStartupScriptRegistered method, which allows you to confirm whether the script has been registered before continuing to use. There are two methods available for this problem:

IsStartupScriptRegistered: Check whether the start script of the client has been registered on the page object. The single parameter is the script name.

IsClientScriptBlockRegistered: Check whether the script block of the client has been registered on the page object. The unique parameter is the script name.

Another solution is to use the RegisterOnSubmitStatement method of the page class. List C repeats the function of list B, however, the difference is that the confirmSubmit method is connected to the commit event on the page rather than the Click Event of the button.

This simple example clearly shows how to include JavaScript script blocks and JavaScript scripts into ASP. NET page control, if you use ASP.. NET controls. This method can help you contact these controls with scripts, but you can also use the register Method to centralize the JavaScript of a project. You can create a class file containing scripts and use them on the page as needed, so that you only need to manage these scripts in one location, it is easier to use these scripts on one or more pages of an application.

New and old

Regardless of the Development Platform, you are unlikely to discard using JavaScript as the client scripting language. JavaScript is the standard for developing client functions, even though ASP. NET Control provides a lot of functions, many times you also need to associate JavaScript with these controls. Fortunately, ASP. NET page class contains many methods to integrate script blocks and methods into pages and their controls.

  1. Introduction to ASP. NET Applications
  2. ASP. NET pre-compiled applications
  3. Overview ASP. NET 2.0 FormView controls
  4. Optimize ASP. NET 2.0 Profile Provider
  5. Analysis on ASP. NET Process Model configuration

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.