Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript integrates ASP.NET controls

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how JavaScript integrates ASP.NET controls". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how JavaScript integrates ASP.NET controls.

Conventional mode

Typically, when a Web application needs a pop-up window or confirmation window or other client functionality, you can create the necessary JavaScript methods and call them when needed. For example, there is a confirmation window in list A that users can use to continue or cancel the submission of the form.

After the submit (submit) button is selected, the confirmSubmit method of JavaScript is called, and this confirmation prompt allows the user to continue with the form submission (select confirm) or cancel (select cancel).

The above code works as planned, but it's not so simple when working on a development platform, such as ASP.NET.

Alternative 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 on ASP.NET 's own development model that provides additional functionality.

Attaching JavaScript to ASP.NET is not as simple as direct HTML/JavaScript, and the ASP.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 script blocks. Client scripts start running after the opening tag of the page object form runat= server > element. Script blocks are submitted for output as objects, 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 script blocks in the page, similar to the RegisterClientScriptBlock method, which starts running before the closing tag of the page object form runat= server > element, and the script block is submitted for 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 the name assigned to the script block, and the key value should be * *. Through this key value, multiple server-controlled instances can request the script block without having to let the script run the output stream again; the second parameter script contains the actual script sent to the customer, 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's VB.NET, C #, J#, or any other language. Listing B shows the ASP.NET sample code written in C #.

The JavaScript method is built by a string variable, and its value is passed to the RegisterClientScriptBlock method as the second parameter, the actual method name is assigned to the ASP.NET button control through the Add method of the object's Attributes property, the JavaScript event is passed as a * parameter, and the method name is the second parameter.

Also, you may notice the use of the IsStartupScriptRegistered method, which allows you to confirm that the script is registered before continuing, and there are two ways to use it:

IsStartupScriptRegistered: verify that the client's initiating script has been registered on the page object, and its single parameter is the name of the script.

IsClientScriptBlockRegistered: verify that the script block of the client has been registered on the page object, and its * * 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 functionality of list B, but the difference is to connect the confirmSubmit method to the submit event of the page rather than the click event of the button.

This simple example clearly illustrates how to include JavaScript script blocks and JavaScript scripts on controls on an ASP.NET page. If you use ASP.NET controls, this method can help you link scripts to these controls, but you can also use the register method to centralize the JavaScript of a project. You can create a class file that contains scripts and use them on the page when needed, so that you only need to manage these scripts in one place. It becomes easier to use these scripts on one or more pages in an application.

At this point, I believe you have a deeper understanding of "how JavaScript integrates ASP.NET controls". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report