While ASP. NET provides a strong platform, developers should not overlook the technology that is mature like JavaScript. In this article, Tony Patton will explain how to integrate JavaScript with ASP. NET control in Web development.
While the Web development platform offers flexibility and many features, you often want or need to rely on existing technologies to accomplish a required task, a good example is ASP. NET, which provides a powerful development platform, but also should not ignore the mature technology like JavaScript, In this article, I'll show you how to connect JavaScript code to an ASP. NET control.
General method
Typically, when a Web application requires a pop-up window or confirmation window or other client functionality, you can create the necessary JavaScript methods and invoke them when needed. For example, a confirmation window is included in list A that users can use to continue or cancel a form's submission.
After the Submit button is selected, the JavaScript Confirmsubmit method is called, which allows the user to proceed with the form submission (select confirmation) or Cancel (select cancel).
The above code can work as planned, but it's not as easy to work on a development platform as ASP.
Asp. NET Replacement options
Asp. NET supports the use of HTML elements such as input buttons and text boxes, but a set of completed control elements on an ASP.
attaching JavaScript to ASP. ASP is not as straightforward as the html/javascript way. NET Programming mode provides a way to attach script blocks to page elements in the basic page class. The following methods can be used to attach or integrate JavaScript into a page or page element:
RegisterClientScriptBlock: Allows your page to contain a script block, client script starts after the Page object form runat= The start tag of the server> element, and the script block submits the output as an object. So you must add two script> element tags.
Registeronsubmitstatement: Allows you to assign a script block/method to the Page object onsubmit event.
RegisterStartupScript: Allows you to include a script block in the page, similar to the RegisterClientScriptBlock method, which starts before the end tag of the Page object form runat= server> element , the script block submits the output as an object, so you must add two script> element tags.
Each method can accept two parameters: the key and the script, the key is assigned to the script block name, the key value should be unique, through this unique key value, multiple server-controlled instances can request the script block, without having the script run the output stream again, the second parameter script contains the actual script sent to the customer, It can be the full JavaScript code or the name of a method.
These methods can be used in the actual ASP. NET page code, either vb.net, C #, J #, or any other language, and List B shows the ASP. Code written in C #.
The JavaScript method is constructed by a string variable whose 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 button control of ASP., and the JavaScript event is passed as the first argument, and the method name is the second argument.
Also, you may notice the use of the IsStartupScriptRegistered method, which allows you to confirm that the script has been registered before you continue to use it, there are two methods available for this issue:
IsStartupScriptRegistered: Verify that the client's start script is already registered on the Page object and that its single parameter is the name of the script.
IsClientScriptBlockRegistered: Verify that the client's script block has been registered on the Page object, and its unique parameter is the name of the script.
Another way to solve the problem is to use the Registeronsubmitstatement method of the page class, and list C repeats the function of list B, but the difference is that the Confirmsubmit method is connected to the page's commit event instead of the button's Click event.
This simple example clearly illustrates how to include JavaScript script blocks and JavaScript scripts on the control of an ASP, if you use ASP. NET controls, which can help you connect scripts to these controls, but you can also use the Register method to centralize JavaScript for a project. You can create a class file that contains scripts and use them in a page when you need them, so that you only have to manage them in one place and it's easier to use them on one or more pages in one application.
Set new and old in a
Regardless of the development platform, you are less likely to abandon the use of JavaScript as a client-side scripting language, and JavaScript is the standard for developing client functionality, although ASP. NET controls provide a lot of functionality, and many times you also need to "marry" JavaScript to these controls, but fortunately the ASP has many ways to integrate script blocks and methods into the page and its controls
Tips for integrating JavaScript in ASP.