Prevent opening multiple windows in ASP.NET

來源:互聯網
上載者:User

本文轉自: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 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.