Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

How to disable right click option for pdf file in asp.net.
Actually i had a button when i click on that button pdf flie wants to be open in separate window.upto this working fine.
but i want to disable right click option for that pdf file.it was not getting....

Please see my code:

Sample1.aspx:

<script language="javascript" type="text/javascript">

function btnWeek_onclick()

{
popupWindow = window.open(

"Sample2.aspx", 'mynewwin', 'height=500,width=600,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no');

}

</script>

<asp:Button ID="btnOpenPDF" runat="server" Text="Open PDF"
OnClientClick ="return btnWeek_onclick()"/>

Sample2.aspx:

<script language="javascript" type="text/javascript">

//Function to disable Cntrl key/right click
function DisableControlKey(e)
{
// Message to display
var message = "Cntrl key/ Right Click Option disabled";
// Condition to check mouse right click / Ctrl key press
if (e.which == 17 || e.button == 2)
{
alert(message);
return false;
}
}
</script>
<body onmousedown="return DisableControlKey(event)">
<form id="form1" runat="server">
<div>
</div>

</form>
</body

Code Behind for (Sample2.aspx)
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType =application/pdf;
Response.Clear();
Response.TransmitFile(E:\\Desire-Determination-Dedication.pdf);
Response.End();
}

Regards,
VenkiDesai.
Posted
Updated 17-Nov-19 18:29pm

Try this thread,
How to disable right click on asp.net web page using JavaScript | Disable mouse right click on webpage[^]

Include this javascript in the head portion:
JavaScript
<SCRIPT LANGUAGE="JavaScript">
<!-- Disable
function disableselect(e){
return false
}
function reEnable(){
return true
}
//if IE4+
document.onselectstart=new Function ("return false")
document.oncontextmenu=new Function ("return false")
//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
//-->
</script>

Visit the following links as well: click here[^]
Found it here[^]
 
Share this answer
 
Comments
Venki Desai 10-Sep-12 2:37am    
Thanks for replying Prasad
i already did the above javascript function but it was not getting.
if you don't mind please see the below link my output is like that
http://www.eggheadcafe.com/community/asp-net/17/10466069/how-to-diable-right-click-option-for-pdf-file-using-aspnet.aspx
for web page i am disbling right click option but for the pdf file i am not getting.
once if you see the above link you can undersatand the output of mine.
I don't want to get right click option for that pdf file what i opened on web page.
Please give the solution for this requirement
Thanks in Advance.
Vaibhav Vats 11-Sep-12 8:41am    
not working
Did you fixed this issue?, i stucked in the same issue, I should disable the right click event in the embed tag which loaded the pdf document.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900