[转]Prevent opening multiple windows in ASP.NET

本文提供了一个 ASP.NET Web 应用程序的示例,展示了如何检测并防止多窗口或标签同时使用。通过在代码中生成唯一随机字符串并将其作为窗口名称,可以实现这一目标。

本文转自:http://code.msdn.microsoft.com/CSASPNETPreventMultipleWind-b1dd2bd6

CSASPNETPreventMultipleWindows Overview
 

Use:
 

The project illustrates how to detect and prevent multiple windows or 
tab usage in Web Applications.
 
Demo the Sample. 

Please follow these demonstration steps below.
 
Step 1: Open the CSASPNETPreventMultipleWindows.sln.
 
Step 2: Expand the CSASPNETPreventMultipleWindows web application and press 
       Ctrl + F5 to show the Default.aspx.
 
Step 3: We will see two links on the page. First, you can Left click one
        of them jump to correct link(Like Nextlink.aspx and Nextlink2.aspx).
 
Step 4: Then, you can right click these links and choose Open In New Tab 
       or Open In New Window,and you will find the link not available.
 
Step 5: you can even copy the right url and Paste it in the browser address bar for
        test.but you can not achieve your goal.
 
Step 6: Validation finished.
 

Code Logical:
 

Step 1. Create a C# "ASP.NET Empty Web Application" in Visual Studio 2010 or
        Visual Web Developer 2010. Name it as "CSASPNETPreventMultipleWindows".
 
Step 2. Add one folder, "UserControls".Add two User Controls in this folder,
        "DefaultPage.ascx","NextPage.ascx".
 
Step 3. Add five web forms in the root directory,"Default.aspx","InvalidPage.aspx"
        ,"Main.aspx","NextLink.aspx","NextLink2.aspx".
 
Step 4. Move DefaultPage.ascx user control on Default.aspx file and move
        NextPage.ascx user control on all other files extension name are ".aspx".
         In Defalut.aspx.cs page, we have a method generate unique random number:
         [code]
        public string GetWindowName()
        {
            string WindowName = Guid.NewGuid().ToString().Replace("-", "");
            Session["WindowName"] = WindowName;
            return WindowName;
        }
         [/code]
         And window.name will recieve this unique string,we use window.name to prevent
         mutiple windows and tabs. 
        [code]
         //set window.name property
        window.open("Main.aspx", "<%=GetWindowName() %>");
 
         //If this window name not equal to sessions,will be goto InvalidPage
       if (window.name != "<%=GetWindowName()%>") {
          window.name = "InvalidPage";
          window.open("InvalidPage.aspx","_self");      
       }
        [/code]
 
Step 5. Write the codes like the sample.you can get more details from comments in
        the sample file.
         
Step 6. Build the application and you can debug it.
 


References:
 

MSDN: User control
 http://msdn.microsoft.com/en-us/library/fb3w5b53.aspx
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值