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.
- 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 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:
- 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 (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.
- 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
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:
- Private Sub btWinOpen_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles btWinOpen. Click
-
- 'Use window. open and registerStartupScript for simple demonstration.
-
- If (Not IsClientScriptBlockRegistered ("OpenScript") Then
- 'Check whether the script has been added. If not, add it.
- Dim strScript As String =""
- RegisterStartupScript ("OpenScript", StrScript)
- End If
- 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.
- Common blocking methods in ASP. NET pop-up window
- Analysis of hidden risks of ASP. NET virtual hosts in file directory management
- Development Analysis of ASP. NET Custom Controls
- Analysis of hidden risks of ASP. NET virtual hosts in displaying files
- ASP. NET host resource control experience