ASP. NET proficient pop-up window

Source: Internet
Author: User

As Microsoft's latest tool for creating dynamic Web websites, ASP. NET has made great strides in changing the original Web programming methods compared with ASP and JSP. Its code and page Separation Technology (CodeBehind) and sophisticated Web server controls provide programmers with a Web server development method that is more in line with traditional programming. However, Web programming still has different characteristics from traditional programming, which determines ASP. NET programming must use some special skills to complete the program requirements. The pop-up window is exactly the representative of this type of programming method. A considerable number of programming books use silence or a phrase to bring the pop-up window, it seems that the pop-up window is a huge use of the world. This article will solve most of the issues in the pop-up window.

To improve the concurrency and throughput of Website access, ASP. NET uses client scripts to relieve the pressure on the server, just like other server scripts. ASP. NET does not support the pop-up window until now (Version 1.1). You must use JavaScript (or VBScript) to use the client pop-up window.

I. Warning window and using client scripts in CodeBehind

To display the simplest warning window in the browser, you can use the JavaScript statement:

Window. alert ([sMessage])

SMessage indicates the prompt information. Unfortunately, this pop-up window has only one "OK" button, which can only serve as a prompt. If you want to pop up a dialog box for asking when you delete a record, you need:

BConfirmed = window. confirm ([sMessage])

BConfirmed is the return value, and sMessage is the prompt information. The pop-up window has two options: "OK" or "discard". The returned value is placed in bConfirmed for the code to determine.

JavaScript and Codehind should be integrated to improve code reusability and readability. There are usually two ways to achieve this effect.

(1) Use the Response. Write method:

The Response. Write method has been supported in the ASP era. It can write code to the client, which is quite convenient and intuitive. The following code demonstrates how to use the Response. Write method to display a warning message.

Private Sub btAlert_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btAlert. Click

Demonstrate the Response. Write method and alert window.

Response. Write ("")

End Sub

(2) Use the RegisterXXX Method

If you observe the HTML code generated by Response. Write, you will find that the code generated by the Response. Write method is written to the beginning of the HTML code, that is, before the tag. At this time, all HTML objects have not yet been generated. If you want to use and interact with the objects in HTML, the error "object not found" will occur. Therefore, we recommend that you use the RegisterXXX method, which is more in line with the CodeBehind method. RegisterXXX includes RegisterClientScriptBlock, RegisterStartupScript, and IsStartupScriptRegistered functions used for judgment.

RegisterStartupScript is prototype:

Overridable Public Sub RegisterStartupScript (_

ByVal key As String ,_

ByVal script As String _

)

Key indicates the unique identifier of the script. script indicates the string of the script.

The prototype of RegisterClientScriptBlock is the same as that of RegisterStartupScript. The two functions differ in writing the script code included into different HTML files. RegisterClientScriptBlock sends a client script immediately after the start mark of the Page object element. RegisterStartupScript sends the script before the end mark of the Page object element. If your script has a statement that interacts with the Page Object (the doucument object) (as shown in the following example), we recommend that you use RegisterStartupScript, if you want to execute the client script as early as possible, you can use RegisterClientScriptBlock or Response. write.

To prevent repeated scripts on the page, the ReisterStartupScript/RegisterClientScriptBlock uses the key as the registered Key during script registration, and then the IsClientScriptBlockRegistered can be used in the program for judgment.

The following example uses RegisterClientScriptBlock to demonstrate how to use confirm.

Private Sub btConfirm_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btConfirm. Click

Demonstrate the RegisterClientScriptBlock method and the confirm window

If (Not IsClientScriptBlockRegistered ("clientScript") Then

'Check whether the script has been added. If not, add it.

Dim strScript As String

StrScript = ""

'Register the script

RegisterClientScriptBlock ("clientScript", strScript)

'If you select "no", continue to the next step.

End If

End Sub

There are two pages in this news. Currently, there are two pages in page 1st.


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.