Technical Analysis of pop-up window in ASP. NET

Source: Internet
Author: User

How is the pop-up window in ASP. NET implemented? 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 also uses the client script to reduce the pressure on the server, that is, ASP. NET. ASP. NET does not support pop-up windows 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 ASP. NET to pop up the window:

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.

 
 
  1. Private Sub btAlert_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btAlert. Click
  2. 'Demonstrate the Response. Write method and alert window.
  3. Response. Write ("")
  4. End Sub

2) use the RegisterXXX method ASP. NET to pop up the window:

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:

 
 
  1. Overridable Public Sub RegisterStartupScript( _  
  2.  ByVal key As String, _  
  3.  ByVal script As String _  
  4. )  

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 (doucument object), we will see 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.

 
 
  1. Private Sub btConfirm_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btConfirm. Click
  2.  
  3. 'Demonstrate the RegisterClientScriptBlock method and the confirm window
  4.  
  5. If (Not IsClientScriptBlockRegistered ("ClientScript") Then
  6. 'Check whether the script has been added. If not, add it.
  7. Dim strScript As String
  8. StrScript ="" 
  9. 'Register the script
  10. RegisterClientScriptBlock ("ClientScript", StrScript)
  11. 'If you select "no", continue to the next step.
  12. End If
  13.  
  14. End Sub

Ii. Specify a page in the pop-up window of ASP. NET

The prompt window is far from meeting our requirements. In the program, we often need to pop up the specified page. In this case, you can use the window. open Method of JavaScript. In combination with the previous RegisterClientSciptBlock method, we can implement the pop-up of the specified page.

The following code pops up a specified page:

 
 
  1. Private Sub btWinOpen_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btWinOpen. Click
  2.  
  3. 'Use window. open and registerStartupScript for simple demonstration.
  4.  
  5. If (Not IsClientScriptBlockRegistered ("OpenScript") Then
  6. 'Check whether the script has been added. If not, add it.
  7. Dim strScript As String ="" 
  8. RegisterStartupScript ("OpenScript", StrScript)
  9. End If
  10. End Sub

The program uses the Window. open Method to pop up a new page, which has only one parameter: the URL address of the new pop-up Window. The fact is that the window. open method has multiple parameters, but this is a simple example of objective cipt. We will not detail it here. If you have any questions, please query MSDN.

This program works normally in IE. However, if you are using browsers such as GoSurf, MyIE2, and NetCapter, unfortunately! You will not see the pop-up window. This is the pop-up window filtering question we will discuss.

3. Non-standard IE browser in ASP. NET pop-up window discussion on pop-up window filtering behavior

The proliferation of advertising windows has led many Internet users to abandon standard IE browsers and use software such as GoSurf, MyIE2, and NetCapter that uses the IE kernel to support multiple pages and automatically shield advertisements. It is said that Microsoft will also add the blocking advertisement Window Function in the forthcoming IE6 sp2. This is of course a good thing for most netizens. For programmers, the pop-up window is not essentially different from the general advertisement, such a window will also be banned by the pop-up window manager, and the result is of course not expected.

The information about the pop-up window in ASP. NET is introduced here. I hope it will help you.

  1. Common blocking methods in ASP. NET pop-up window
  2. Analysis of hidden risks of ASP. NET virtual hosts in file directory management
  3. Development Analysis of ASP. NET Custom Controls
  4. Analysis of hidden risks of ASP. NET virtual hosts in displaying files
  5. ASP. NET host resource control experience

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.