Proficient in ASP.NET pop up window technology

  • 2020-05-17 05:17:35
  • OfStack

This paper also discusses the filtering behavior of the non-standard IE browser on the popup window and the corresponding countermeasures for the use of the popup window, in order to provide a general and optimal scheme for the use of the popup window.

As the latest Microsoft tool for building dynamic Web websites, ASP.NET is a significant improvement over ASP and JSP in terms of changing the original Web programming. Its code and page separation technology (CodeBehind) and sophisticated Web server controls provide programmers with a more traditional programming approach to Web server-side development. However, Web programming still has different features from traditional programming, which determine that ASP. NET programming must use some special skills to complete the program requirements, and the pop-up window is the representative of this programming style. Quite a few programming books are silent or silent about pop-ups, seemingly unable to see the huge use of pop-ups. This article will help you solve most of the problems in using pop-up Windows.

In order to improve the concurrency and throughput of website visits, ASP.NET, like other server scripts 1, also USES client scripts to relieve server stress. ASP.NET does not support pop-ups directly up to now (version 1.1), but must use client pop-ups via JavaScript (or VBScript).

1. Warning window and way of using client script in CodeBehind

To pop up the simplest warning window in your browser, use the JavaScript statement:

window.alert( [sMessage])

Among them, sMessage is the prompt message. Unfortunately, such a popup window has only one "ok" button, which can only serve as a prompt. If we want to pop up a query window when deleting records, you need to use:

bConfirmed = window.confirm( [sMessage])

Where: bConfirmed is the return value and sMessage is the prompt message. This pop-up window has two options: "ok" or "give up", and the return value of its selection is placed in bConfirmed for the code to determine.

To make your code more reusable and readable, you should mix JavaScript with Codehind. There are usually two ways to achieve this effect.

(1) use Response.Write method:

The Response.Write method has been supported since the days of ASP. It is a very convenient and intuitive way to write code to clients. The following code demonstrates how to display a warning message using the Response.Write method.
 
Private Sub btAlert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAlert.Click 
 ' demo Response.Write Methods and alert Window.  
 Response.Write(" ") 
End Sub 


(2) use RegisterXXX method

If you look at the Response.Write generated HTML code, you will see that the code generated by the Response.Write method is written to the very beginning of the HTML code, before the label. At this point, none of the HTML objects have yet been generated, and if you want to use and interact with the HTML objects, you will get the "can't find object" error. Therefore, I recommend one approach that is more in line with the CodeBehind approach -- using the RegisterXXX approach. RegisterXXX includes: RegisterClientScriptBlock, RegisterStartupScript and IsStartupScriptRegistered function for judgment.

The prototype of RegisterStartupScript is:
 
Overridable Public Sub RegisterStartupScript( _ 
 ByVal key As String, _ 
 ByVal script As String _ 
) 

Where: key represents the 1-only identity of the script, and script is the string representing the script.

The prototype of RegisterClientScriptBlock is the same as that of RegisterStartupScript, but the two functions differ in that the script code they contain is written to a different location in the HTML file. RegisterClientScriptBlock issues the client script immediately after the start tag of the element of the Page object, and RegisterStartupScript before the end tag of the element of the Page object. If your script has statements that interact with page objects (doucument objects) (as we'll see later in the example), RegisterStartupScript is recommended, whereas if you want the client script to execute as early as possible, you can use RegisterClientScriptBlock or Response.Write.

In order to prevent the repeated addition of script to the page, ReisterStartupScript/RegisterClientScriptBlock used key as the registered Key when registering the script, and IsClientScriptBlockRegistered can be used for judgment in the program.

The following example USES RegisterClientScriptBlock to demonstrate the use of confirm.
 
Private Sub btConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btConfirm.Click 

 ' demo RegisterClientScriptBlock Methods and confirm window  

 If (Not IsClientScriptBlockRegistered("clientScript")) Then 
 'determine if you are already in the script, and if you are not, join the script.  
  Dim strScript As String 
  strScript = " " 
 'register script'  
  RegisterClientScriptBlock("clientScript", strScript) 
 If "no" is selected, proceed to execution.  
 End If 

End Sub 



2. Pop up the specified page

The prompt window alone is far from enough to satisfy our requirements, in the program, we often need to pop up the specified page. At this point you can use the window.open method of JavaScript. With the previous RegisterClientSciptBlock method, we can pop up the specified page.

The following code shows how to pop up the specified page:

 
Private Sub btWinOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btWinOpen.Click 

 ' use window.open with registerStartupScript Simple demonstration.  

 If (Not IsClientScriptBlockRegistered("OpenScript")) Then 
  ' Determine if the script is already in, and if not, in.  
  Dim strScript As String = " " 
  RegisterStartupScript("OpenScript", strScript) 
 End If 
End Sub 


The program USES the Window.open method to pop up a new page with just 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 the simple content of javascipt, which we will not go into here. If you have any questions, please refer to MSDN.

This procedure is normal in IE when using 1 cut directly. Unfortunately, if you're using a browser like GoSurf, MyIE2, NetCapter, etc. You won't see any pop-ups. This is what we're going to talk about with pop-up filtering.

3. Discussion of non-standard IE browser's filtering behavior for pop-up Windows

The proliferation of advertising Windows has caused many netizens to abandon the standard IE browser in favor of software such as GoSurf, MyIE2 and NetCapter, which USES the IE kernel to support multiple pages and can automatically block advertisements. Microsoft is also said to be adding the ability to block ads in the upcoming IE6 sp2. This is of course a good thing for most netizens, but for programmers, the way we use pop-up Windows is not fundamentally different from the way we use ads. Such Windows will also be indiscriminately blocked by the pop-up window manager, with the result that we don't want to see. Is there a standard way to make a window pop up properly? This requires us to understand how browsers block ads. Common AD blockers use the following three ways to filter ads:

(1) window cap-based blocking method

The idea behind this block is to periodically check all IE window titles, then compare them with the existing list (a list of arrays maintained by the program), and close the window if they are the same. Obviously, this approach has many drawbacks, it blocks all pop-up Windows, the tube is too dead, in the program really used very little. However, it is quite common to use it in terms of the way it transforms. That is, based on the window title name of the intelligent filtering technology, it is based on the pop-up window title contains keywords about advertising to block, which makes a good exploration to improve the filtering effect.

(2) blocking method based on window class and location

After analysis, it is found that the class names of the normal browsing window are IEFRAME and CabinetWClass, while the class names of the advertising window are CabinetWClass. Further analysis found that: the WorkerA class of the advertising window and Shell DocObject View rect. top are the same, and the WorkerA class of the normal IE window and Shell DocObject View View View rect. top are not the same. Based on the above two you can write an AD killer program. In fact, I'm skeptical about the universality of the program. Because I used Spy++ analysis, I found that in Windows2000(the operating system I used), the IE window class is IEFrame. At the same time, since Win2000 is an operating system based on Unicode code, there is no WorkerA class, but WorkerW class instead. At the same time, rect.top is not the same. Since the author does not have WindowsXP operating system, it is not possible to conduct further experiments on WindowsXP.

(3) blocking method based on IE COM component

The above two ways are to treat the IE window as a common Windows window, to judge. In fact, IE is a typical COM component-based browser. All IE kernel-based browsers wrap the shdocvw.dll file and then write the BHO code. Only in this way can you really control the IE browser, instead of just scratching the surface with method 1.2.

There is also a pop-up window blocking method based on the IE kernel. It can intercept pop-ups before they open. The principle is that every time IE opens a new window, the NewWindow event is triggered, and the OnNewWindow2([out] IDispatch*, [out] BOOL *bCancel) method is executed. Reload this method to determine if a new window opening event has occurred after the browser page has been downloaded. If so, it means normal popup window, otherwise try to intercept.

Since browsers like Gosurf themselves overload the Shocvm.dll component, it is natural to use the third method. In the process of use, however, it is sometimes found that advertising filtering is not perfect, but the principle is basically the same.

Related articles: