Author: Builder, Tony Patton
Although ASP.NET provides a powerful 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 web development platforms provide flexibility and numerous features, you often want or need to rely on existing technologies to complete a necessary task. A good example is ASP.NET, which provides a powerful development platform, but at the same time Mature technologies like JavaScript should not be ignored either. In this article, I will explain to you how to connect JavaScript code to ASP.NET controls.
conventional way
Typically, when a Web application requires a popup or confirmation window or other client-side functionality, you create the necessary JavaScript methods and call them when needed. For example, Listing A contains a confirmation window that the user can use to continue or cancel form submission.
After selecting the submit button, JavaScript's confirmSubmit method is called. This confirmation prompt allows the user to proceed with form submission (select Confirm) or cancel (select Cancel).
The above code works as planned, but it's not that simple when working on a development platform, such as ASP.NET.
Replacement options for ASP.NET
ASP.NET supports the use of HTML elements such as input buttons and text boxes, but there is a complete set of control elements that provide additional functionality based on ASP.NET's own development model.
Attaching JavaScript to ASP.NET is not as simple as the direct HTML/JavaScript approach. The ASP.NET programming model provides methods in the base page class to attach script blocks to page elements. The following methods can be used to attach or integrate JavaScript into a page or page element:
l
RegisterClientScriptBlock: Allows your page to contain script blocks, client-side scripts in the page object <form
runat=
It starts running after the opening tag of the server> element. The script block submits output as an object, so you must add two <script> element tags.
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 in the page, similar to the RegisterClientScriptBlock method. This method is in the page object <form
runat=
Running before the closing tag of the server> element, the script block submits output as an object, so you must add two <script> element tags.
Each method can accept two parameters: key and script. The key is the name assigned to the script block. The key value should be unique. With this unique key value, multiple server-controlled instances can request the script block. , without having to run the script again on the output stream; the second parameter script contains the actual script sent to the client, which can be the complete JavaScript code or the name of a method.
These methods can be used in actual ASP.NET page code, whether it is VB.NET, C#, J# or any other language. Listing B shows the ASP.NET sample code written in C#.
JavaScript methods are constructed via a string variable, its value is passed to the RegisterClientScriptBlock method as the second argument, and the actual method name is passed through the object's Attributes
The Add method of the property is assigned to the ASP.NET button control. The JavaScript event is passed as the first parameter, and the method name is the second parameter.
Moreover, you may notice the use of the IsStartupScriptRegistered method, which allows you to confirm whether the script has been registered before continuing to use it. There are two methods for this problem:
l
IsStartupScriptRegistered: Confirms whether the client's startup script has been registered on the page object. Its single parameter is the name of the script.
l
IsClientScriptBlockRegistered: Confirms whether the client's script block has been registered on the page object. Its only parameter is the name of the script.
Another way to solve the problem is to use the RegisterOnSubmitStatement method of the page class. Listing C repeats the function of Listing B, but the difference is that the confirmSubmit method is connected to the submit event of the page instead of the click event of the button.
This simple example clearly illustrates how to include JavaScript script blocks and JavaScript scripts into controls on an ASP.NET page. If you use ASP.NET controls, this approach can help you attach scripts to those controls, but You can also use the register method to centralize a project's JavaScript. You can create a class file that contains the scripts and use them in the pages as needed. This way you only need to manage the scripts in one place and use them on one or more pages in an application. becomes easier.
A combination of old and new. No matter which development platform you are on, you are unlikely to give up using JavaScript as a client-side scripting language. JavaScript is the standard for developing client-side functions. Although ASP.NET controls provide a large number of functions, many times you still JavaScript needs to be "married" to these controls. Fortunately, the ASP.NET page class contains numerous methods to integrate script blocks and methods into the page and its controls.