Proficient in ASP. NET pop-up window Technology)

Source: Internet
Author: User
Abstract: This article discusses how to use codebehind in ASP. NET to implement various pop-up windows and interact with the pop-up windows. It also explores the filtering behaviors of pop-up windows by common non-standard ie browsers and the corresponding countermeasures for using the pop-up window, in order to provide a general and better solution for using the pop-up window.

Keywords: ASP. NET, codebehind, filter, COM interface, JavaScript, binding

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. ItsCodeThe codebehind technology and the complete Web server control are as follows:ProgramProvides 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

2. The specified page is displayed.

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.

Iii. Non-standard IE browser's 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. Is there a standard way to make the window pop up normally? This requires us to understand the principle of blocking advertisements by browsers. Generally, the ad blocker uses the following three methods to filter advertisements:

(1) Blocking Based on window titles

The principle of this blocking method is to regularly check all the title of the IE window, and then compare it with the existing list (an array list maintained by the Program). If there is the same, close this window. Obviously, this method has many defects. It blocks all the pop-up windows, and is too hard to manage. It is rarely used in the program. However, the deformation method based on it is quite common. That is, the smart Filtering Technology Based on the title name of the window, which blocks ads based on whether the title of the pop-up window contains keywords, which makes a good exploration to improve the filtering effect.

(2) Blocking Based on the window class and Position

After analysis, we found that the class names of the normal window are ieframe and cabinetwclass, while the class names of the window for advertisement are cabinetwclass. Further analysis shows that the workera class of the AD window and the rect of the shell docobject View class are displayed. the top value is the same. The workera class of the normal ie window and rect of the shell docobject View class. the top values are different. Based on the above two points, you can write the advertisement killer program. In fact, I am skeptical about the versatility of this program. The analysis using spy ++ found that in Windows2000 (the operating system used by the author), the classes of IE Windows are ieframe. At the same time, because Win2000 is an operating system based on Unicode Code, there is no workera class, and it is replaced by the workerw class. At the same time, the rect. Top is not the same. Because I do not have a Windows XP operating system, I cannot perform further experiments on Windows XP.

(3) Blocking Based on ie com components

Both of the above methods treat IE Windows as a normal Windows window and make judgments. In fact, ie is a typical com-based browser. All ie-based browsers package the shdocvw. dll file and then write the corresponding BHO code. Only in this way can we truly control ie browsers, rather than the itching of methods 1 and 2.

There is also a pop-up window blocking method based on the IE kernel. It can be intercepted before the pop-up window is opened. 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 whether a new window event occurs after the page has been downloaded. If yes, it indicates a normal pop-up window, and vice versa.

As a browser like gosurf itself reloads the shocvm. dll component, the use of the third method naturally becomes a matter of course. However, during use, you may find that ad filtering is not perfect, but the principle is basically the same.

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.