Abstract: This article discusses how to implement various pop-up windows using CodeBehind in ASP.NET and interact with pop-up windows. It also discusses the various filtering behaviors of commonly used non-standard IE browsers for pop-up windows and the corresponding countermeasures for using pop-up windows, in order to provide a universal and better solution for using pop-up windows.
Keywords: ASP.NET, CodeBehind, filtering, COM interface, JavaScript, binding.
As Microsoft's latest tool for building dynamic Web sites, ASP.NET has made great progress in changing the original Web programming method compared to ASP and JSP. . Its code and page separation technology (CodeBehind) and complete Web server controls provide programmers with a Web server-side development method that is more in line with traditional programming. However, Web programming still has characteristics that are different from traditional programming. These characteristics determine that some special techniques must be used in ASP.NET programming to complete program requirements. Pop-up windows are the representative of this type of programming method. Quite a few programming books are silent about pop-up windows or just mention them in one word, seeming to fail to understand the huge world of use of pop-up windows. This article will solve most of the problems in using pop-up windows for you.
In order to improve the concurrency and throughput of website access, ASP.NET, like other server scripts, also uses client-side scripts to reduce the pressure on the server. ASP.NET does not directly support pop-up windows until now (version 1.1), and client-side pop-up windows must be used through JavaScript (or VBScript).
1. Warning window and the method of using client-side script in CodeBehind.
To pop up the simplest warning window in the browser, you can use the JavaScript statement:
window.alert([sMessage])
where sMessage is the prompt message. Unfortunately, such a pop-up window only has an "OK" button and can only serve as a prompt. If we want to pop up a query pop-up window when deleting a record, 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 choice is placed in bConfirmed for the code to make a judgment.
In order to improve the reusability and readability of code, JavaScript and Codehind should be integrated with each other. There are usually two ways to achieve this effect.
(1) Use the Response.Write method:
Using the Response.Write method has been supported as early as the ASP era. It can write code to the client, which is a very convenient and intuitive method. 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
' Demonstrates 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 very beginning of the HTML code, that is, before the tag. At this time, all HTML objects have not been generated yet. If you want to use and interact with objects in HTML, an "object not found" error will occur. Therefore, the author recommends a method that is more in line with the CodeBehind method-using the RegisterXXX method. RegisterXXX includes: RegisterClientScriptBlock, RegisterStartupScript and the 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 unique identifier of this script, and script is a string representing the script.
The prototype of RegisterClientScriptBlock is the same as RegisterStartupScript. The difference between the two functions is that the script code they contain is written to different locations in the HTML file. RegisterClientScriptBlock emits the client script immediately after the opening tag of the element of the Page object, and RegisterStartupScript emits the script before the closing tag of the element of the Page object. If your script has statements that interact with the page object (document object) (this will be seen in our later examples), it is recommended to use RegisterStartupScript. On the other hand, if you want the client script to be executed as early as possible, you can use RegisterClientScriptBlock. Or Response.Write.
In order to prevent repeated addition of scripts to the page, ReisterStartupScript/RegisterClientScriptBlock uses key as the registration key when registering the script, and then IsClientScriptBlockRegistered can be used to make judgments in the program.
The following example will use RegisterClientScriptBlock to demonstrate the use of confirm.
Private Sub btConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btConfirm.Click
'Demonstrate the RegisterClientScriptBlock method and confirm windowIf
(Not IsClientScriptBlockRegistered("clientScript")) Then
'Determine whether the script has been added, if not, add it.
Dim strScript As String
strScript = " "
'Register scriptRegisterClientScriptBlock("clientScript", strScript)
'If you select "No", continue execution.
End If
End Sub
2. Pop up the specified page.
Just having a prompt window is far from meeting our requirements. In the program, we often need to pop up the specified page. At this time, you can use JavaScript's window.open method. 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 and registerStartupScript for a simple demonstration.
If (Not IsClientScriptBlockRegistered("OpenScript")) Then
'Determine whether the script has been added, if not, add it.
Dim strScript As String = " "
RegisterStartupScript("OpenScript", strScript)
End If
End Sub
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. It turns out that the window.open method has multiple parameters, but this is a simple matter of javascipt and we won't go into details here. If you have related questions, please check MSDN.
This program works fine when used directly in IE. But if you are using browsers such as GoSurf, MyIE2, NetCapter, etc., then, unfortunately! You won't see pop-ups. This is what we’re going to discuss about pop-up filtering.
3. Discuss the pop-up window filtering behavior of non-standard IE browsers.
The proliferation of advertising windows has made many netizens unbearable to be harassed by advertisements. They have given up on standard IE browsers and used browsers such as GoSurf, MyIE2, and NetCapter that use the IE kernel to support multiple pages and Software that automatically blocks ads. It is said that in the upcoming IE6 sp2, Microsoft will also add the function of blocking advertising windows. This is of course a good thing for most Internet users, but for programmers, the way we use pop-up windows is not fundamentally different from ordinary advertisements. Such windows will also be blocked indiscriminately by the pop-up window manager. The result Of course we don't want to see it. Is there a standard way to make the window pop up normally? This requires us to understand the principle of browser blocking advertising. Common ad blockers use the following three methods to filter ads:
(1) Window title-based blocking method.
The principle of this blocking method is to regularly check all IE window titles, and then filter them based on the existing list (maintained by the program). An array list) to compare, if there is the same, we close this window. Obviously, this method has many flaws. It blocks all pop-up windows and is too strict. It is rarely used in the program. However, the deformation method based on it is quite common. That is, the intelligent filtering technology based on the window title name blocks pop-up windows based on whether the title contains keywords related to advertising. This is a good exploration to improve the filtering effect.
(2) Blocking method based on window class and location.
After analysis, it was found that the class names of normal browsing windows are IEFRAME and CabinetWClass, while the class name of advertising windows is CabinetWClass. Further analysis found that: the values of rect.top of the WorkerA class of the advertising window and the Shell DocObject View class are the same, but the values of rect.top of the WorkerA class of the normal IE window and the Shell DocObject View class are different. Based on the above two points, you can write an advertising killer program. In fact, I'm skeptical about the generalizability of this program. Because the author used Spy++ to analyze and found that in Windows2000 (the operating system used by the author), the classes of IE windows are all IEFrame. At the same time, since Win2000 is an operating system based on Unicode code, there is no WorkerA class and is replaced by the WorkerW class. At the same time, there is no situation where rect.top is different. Since the author does not have a WindowsXP operating system, I cannot conduct further experiments on WindowsXP.
(3) Blocking methods based on IE COM components.
The above two methods treat the IE window as an ordinary Windows window and make judgments. In fact, IE is a typical browser based on COM components. All browsers based on the IE core package the shdocvw.dll file and then write the corresponding BHO code. Only in this way can we truly control the IE browser, instead of scratching the surface like methods one and two.
There is also a pop-up window blocking method based on the IE kernel. It blocks pop-ups before they open. The principle is: Whenever IE opens a new window, the NewWindow event will be triggered and the OnNewWindow2([out] IDispatch*, [out] BOOL *bCancel) method will be executed. Overload this method to determine whether the new window opening event occurs after the browsing page has been downloaded. If it is, it means it is a normal pop-up window, otherwise it will be intercepted.
Since browsers like Gosurf overload the Shocvm.dll component, it is natural to use the third method. However, during use, we sometimes find that ad filtering is not perfect, but the principle is basically the same.