Easily integrate JavaScript on the ASP.net page

Source: Internet
Author: User
Tags contains continue connect window
Asp.net|javascript| page

Author: Builder, Tony Patton

Although ASP.net provides a strong platform, developers should not overlook technologies such as JavaScript that are mature. In this article, Tony
Patton will explain how JavaScript is integrated with asp.net controls in web development.


While the Web development platform offers flexibility and versatility, 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 should not overlook a technology as mature as JavaScript, and in this article I'll show you how to connect JavaScript code to a ASP.net control.


Normal way


Typically, when a Web application requires a pop-up window or confirmation window or other client features, you can create the necessary JavaScript methods and call 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 has been selected, the Confirmsubmit method of JavaScript is invoked, which prompts the user to continue the form submission (select confirmation) or Cancel (select cancel).


The code above can work as planned, but it's not as simple as working on a development platform, like asp.net.


Asp. NET substitution options


Asp. NET supports the use of HTML elements such as input buttons and text boxes, but a complete set of control elements on ASP.net's own development model can provide additional functionality.


Attaching JavaScript to asp.net is not as simple as the direct html/javascript approach, ASP. NET Programming mode provides a way to attach a script block to a page element in a basic page class. The following methods can be used to attach or consolidate JavaScript into a page or page element:


L
RegisterClientScriptBlock: Allow your page to contain script block, client script in Page object <form
runat=
Server> the start tag of the element, the script block is used as an object to submit the output, so you must add two <script> element tags.


L
Registeronsubmitstatement: Allows you to assign a script block/method to the Page object onsubmit event.


L
RegisterStartupScript: Allows you to include a script block on a page, similar to the RegisterClientScriptBlock method, which is in the Page object <form
runat=
Server> the end tag of the element before it runs, the script block is the object that submits the output, so you must add two <script> element tags.


Each method can accept two parameters: keys and scripts, the key is the name assigned to the script block, and the key value should be unique, with this unique key value, multiple server-controlled instances can request a 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 either full JavaScript code or the name of a method.


These methods can be used in the actual asp.net page code, whether in vb.net, C #, J #, or any other language, listing B shows the ASP.net sample code written in C #.


The JavaScript method is constructed from 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
property to the ASP.net button control, 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 using it, and there are two methods available for this problem:


L
IsStartupScriptRegistered: Verify that the client's originating script has been registered on the Page object, and that its single parameter is the name of the script.


L
IsClientScriptBlockRegistered: Confirms that the client's script block has been registered on the Page object, and 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, List C repeats the function of list B, but the difference is to connect the Confirmsubmit method to the page's commit event instead of the button's Click event.


This simple example clearly illustrates how JavaScript script blocks and JavaScript scripts can be included in a control on a asp.net page, and if you use the ASP.net control, this approach helps you to connect scripts to those controls, but you can also use the registration (register) method to set JavaScript for a project in. You can create a class file that contains scripts and use them in a page if you want, so that you need to manage them in one place, and it becomes 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 unlikely to give up using JavaScript as a client-side scripting language, and JavaScript is the standard for developing client functionality, although the ASP.net controls provide a lot of functionality, and many times you need to use JavaScript with these controls "Marriage," Fortunately asp.net page classes contain numerous ways to integrate script blocks and methods onto a page and its controls.



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.