Tuesday, 24 December 2013

How to disable right click on asp.net web page using JavaScript | Disable mouse right click on webpage

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample to Disable Right Click of Page</title>
<script language="JavaScript" type="text/javascript">
//Message to display whenever right click on website
var message = "Sorry, Right Click have been disabled.";
function click(e) {
if (document.all) {
if (event.button == 2 || event.button == 3) {
alert(message);
return false;
}
}
else {
if (e.button == 2 || e.button == 3) {
e.preventDefault();
e.stopPropagation();
alert(message);
return false;
}
}
}
if (document.all) {
document.onmousedown = click;
}
else {
document.onclick = click;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

Monday, 1 July 2013

how to find ip address in asp.net behind proxy in c# vb.net

Introduction: 

In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c# and vb.net.

Description:
  
In previous posts I explained JavaScript Get User IP Address, City, Latitude, Longitude, how to find client machine IP using jquery in asp.nt relating to JQuery. Now I will explain how to get system or client IP address of machine from which the user visited the website in 
asp.net in c# and vb.net. To implement this one write the code as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get visitors machine IP in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblIP" runat="server" Font-Bold="true" Font-Size="XX-Large" ForeColor="#529E00"/>
</div>
</form>
</body>
</html>
Now write the following code in code behind

C# Code


protected void Page_Load(object sender, EventArgs e)
{
string IPAdd = string.Empty;
IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAdd))
IPAdd = Request.ServerVariables["REMOTE_ADDR"];
lblIP.Text = IPAdd;
}
VB.NET Code


Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim IPAdd As String = String.Empty
IPAdd = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If String.IsNullOrEmpty(IPAdd) Then
IPAdd = Request.ServerVariables("REMOTE_ADDR")
End If
lblIP.Text = IPAdd
End Sub
End Class
If you observe above code when users behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to checkHTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address.

Demo

Your IP Address:

180.151.55.134

Simple code to create instance at runtime.

using System; 
using System.Collections.Generic; 
using System.Text; 
 
namespace PSNameSpace 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            PSFactory psFactory = PSFactory.GetInstance(); 
            psFactory.Technique = "Late Binding"; 
            Console.WriteLine("Object created through {0} Technique",psFactory.Technique); 
            Console.ReadLine(); 
        } 
    } 
    public sealed class PSFactory 
    { 
        public string Technique; 
        public PSFactory() 
        { 
        } 
        public static PSFactory GetInstance() 
        { 
            string className = "PSNameSpace.PSFactory"; 
            Object psObject; 
            try 
            { 
                Type t = Type.GetType(className); 
                psObject = Activator.CreateInstance(t); 
            } 
            catch (Exception ex) 
            { 
                throw ex; 
            } 
            return (PSFactory)psObject; 
        } 
    } 
 
} 

Friday, 21 June 2013

App_Offline.htm, taking site down for maintenance

There is much simpler and graceful way to shut down a site while doing upgrade to avoid the problem of people accessing site in the middle of a content update.

Basically, if you place file with name 'app_offline.htm' with below contents in the root of a web application directory, ASP.NET will shut-down the application,  and stop processing any new incoming requests for that application. 
ASP.NET will also then respond to all requests for dynamic pages in the application by sending back the content of the app_offline.htm file (for example: you might want to have a “site under construction” or “down for maintenance” message).

Then after upgrade, just rename/delete app_offline.htm file…and the site would be back to normal.
Just remember that the size of the file should be greater than 512 bytes, doesn't matter even if you add some comments to it to push the byte size as long as it's of the size greater than 512 - it'll work fine.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Maintenance Mode - Outage Message</title>
</head>
<body>
    <h1>Maintenance Mode</h1>
    <p>We're currently undergoing scheduled maintenance. We will come back very shortly.</p>

    <p>Sorry for the inconvenience!</p>
   
<!--       
    Adding additional hidden content so that IE Friendly Errors don't prevent
    this message from displaying (note: it will show a "friendly" 404
    error if the content isn't of a certain size).
   
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
    <h2>Site under maintenance...</h2> 
      
-->
</body>
</html>

Monday, 3 June 2013

JavaScript Validation for Registration form

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaScriptValidations.aspx.cs" Inherits="JavaScriptValidations" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script language="javascript" type="text/javascript">
function validate()
{
      if (document.getElementById("txtName").value=="")
      {
                 alert("Name Feild can not be blank");
                 document.getElementById("txtName").focus();
                 return false;
      }
      if (document.getElementById("txtEmail").value == "")
      {
                 alert("Email id can not be blank");
                 document.getElementById("txtEmail").focus();
                return false;
      }
     var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
     var emailid = document.getElementById("txtEmail").value;
     var matchArray = emailid.match(emailPat);
     if (matchArray == null)
    {
               alert("Your email address seems incorrect. Please try again.");
               document.getElementById("txtEmail").focus();
               return false;
           }

           //WebUrl Validation
 
    if (document.getElementById("txtWebURL").value == "")
    {
               alert("Web URL can not be blank");
               document.getElementById("txtWebURL").value = "http://"
               document.getElementById("txtWebURL").focus();
               return false;
    }
    var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
    var tempURL = document.getElementById("txtWebURL").value;
    var matchURL=tempURL.match(Url);
     if(matchURL==null)
     {
               alert("Web URL does not look valid");
               document.getElementById("txtWebURL").focus();
               return false;
           }

           //Zip Validation
   
     if (document.getElementById("txtZIP").value == "")
     {
               alert("Zip Code is not valid");
               document.getElementById("txtZIP").focus();
               return false;
     }
     var digits="0123456789";
     var temp;
     for (var i = 0; i < document.getElementById("txtZIP").value.length; i++)
     {
         temp = document.getElementById("txtZIP").value.substring(i, i + 1);
               if (digits.indexOf(temp)==-1)
               {
                        alert("Please enter correct zip code");
                        document.getElementById("txtZIP").focus();
                        return false;
               }
     }
     return true;
}
</script>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
    Email:<asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox><br />
    Web URL : <asp:TextBox ID="txtWebUrl" runat="server" ></asp:TextBox><br />
    Zip : <asp:TextBox ID="txtZip"  runat="server"></asp:TextBox> <br />
    <asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" /><br />
    </div>
    </form>
</body>
</html>

Tuesday, 5 March 2013

LINQ - How to change value in select or foreach loop?


var items = from rr in _dt.AllItems 
              where rr.ID == ItemID 
              select new
              {
                  rr.Item, 
                  rr.SecondItem == true ? "On" : "Off"
              };