Author: builder, Tony Patton
Although ASP. NET provides a strong platform, developers should not ignore mature technologies such as JavaScript. In this articleArticleMedium, 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 howCodeConnect to the 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.
Appending 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 scripts.ProgramBlock to the page element. The following methods can be used to append or integrate Javascript into pages or page elements:
L
Registerclientscriptblock: allows your page to contain script blocks. client scripts are stored on the page object <form
Runat =
Server> starts running after the starting label of the element. The script block is submitted and output as an object. Therefore, you must add two <SCRIPT> element labels.
L
Registeronsubmitstatement: allows you to assign a script block/method to the onsubmit event of the page object.
L
Registerstartupscript: allows you to include script blocks on the page. This method is similar to the registerclientscriptblock method.
Runat =
Server> starts running before the end label of the element. 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 based on the attributes of the object.
The add method of the property is assigned to the button control of ASP. NET. The javascript event is passed as the first parameter, and 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:
L
Isstartupscriptregistered: Check whether the start script of the client has been registered on the page object. The single parameter is the script name.
L
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
no matter which development platform you use, you are unlikely to give up using JavaScript as the client script 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.