[ASP.NET]利用oncontextmenu與onSelectStart防止網頁按右鍵與選取

[ASP.NET]利用oncontextmenu與onSelectStart防止網頁按右鍵與選取

最近剛好有一個這樣的需求,這個需求不是在防止別人偷看原始碼..

要防止IE按右鍵與選取最簡單的方法就是在Body加入下列語法

1 <body onDragStart="return false" oncontextmenu="return false" onSelectStart="return false" >

 

也可以用asp.net程式動態加入這些語法
asp.net(c#)
Default.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02   
03 <html>
04 <head runat="server">
05     <title></title>
06 </head>
07 <body id="body" runat="server">
08     <form id="form1" runat="server">
09     <div>
10         F6 Team - Puma
11     </div>
12     </form>
13 </body>
14 </html>

Default.aspx.cs
01 using System;
02   
03 public partial class _Default : System.Web.UI.Page
04 {
05     protected void Page_Load(object sender, EventArgs e)
06     {
07         if (!IsPostBack)
08         {
09             //防右鍵
10             this.body.Attributes.Add("oncontextmenu", "return false");
11   
12             //防選取
13             this.body.Attributes.Add("onSelectStart", "return false");
14   
15             this.body.Attributes.Add("onDragStart", "return false");
16         }
17     }
18 }

 

 執行結果:

推到 Twitter!
posted @ 2010-08-27 09:28  peterlee  阅读(323)  评论(0)    收藏  举报