Pages

Wednesday, October 27, 2010

Site Down For Maintenance - app_offline.htm

Sometimes we require to display the Site Under Construction message to the visitor while we are fixing bugs, upgrading features etc. For this just place the app_offline.htm on the root directory of the application.When ASP.Net sees that it will shut down the application and stop processing any new incoming request for that application. ASP.Net will send the content of this file "app_offline.htm" for all the incoming request. This file must have at least 512 bytes of content within it. If you do not have that content just place some content and comment them.

Tuesday, October 19, 2010

Display country with flags in the dropdown using javascript

Sometime it is required to display images with the text in the dropdown.
Here is the code to display the images in the dropdown.
Create a folder "flags" and place the flag images there.

<%@ Page Language="C#" AutoEventWireup="true"  %>
<!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">
    <title>Display images in dropdown</title>
    <script type="text/javascript">
    function SetFlag()
    var ddlCountry=document.getElementById('ddlCountry');              
    var iso = ddlCountry.options[ddlCountry.selectedIndex].value; 
  if (iso.length > 0)
  {
    document.getElementById('imgFlag').src =  "flags/" + iso + ".png";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundImage = "url(flags/" + iso + ".png" +")";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundRepeat="no-repeat";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundPosition="right";    
  }
  else
    document.getElementById('imgFlag').src =  "flags/pixel.gif";
}
function getDropdownListTextWithImage()
{
   var ddlCountry=document.getElementById('ddlCountry');       
    for(var j = 0, limit1 = ddlCountry.options.length; j < limit1;  j++)
    {
        ddlCountry.options[j].style.backgroundImage = "url(flags/" + ddlCountry.options[j].value + ".png" +")";
        ddlCountry.options[j].style.backgroundRepeat="no-repeat";
        ddlCountry.options[j].style.backgroundPosition="right";       
    }
}
    </script>
</head>
<body onload="SetFlag();getDropdownListTextWithImage();">
    <form id="form1" runat="server">
    <div style="background-repeat:no-repeat;background-position:top right">
     <asp:DropDownList runat="server" ID="ddlCountry" onchange="SetFlag();" onkeydown="SetFlag();" onkeyup="SetFlag();" TabIndex="4" EnableViewState="false" ValidationGroup="AddComment"
     >
     <asp:ListItem value="">[Not specified]</asp:ListItem>
    <asp:ListItem value="al" >Albania </asp:ListItem>
    <asp:ListItem value="dz">Algeria</asp:ListItem>
    <asp:ListItem value="ar">Argentina</asp:ListItem>
    <asp:ListItem value="am">Armenia</asp:ListItem>
    <asp:ListItem value="au">Australia</asp:ListItem>
    <asp:ListItem value="at">Austria</asp:ListItem>    
     </asp:DropDownList>&nbsp;
  <asp:Image runat="server" ID="imgFlag" AlternateText="Country flag" Width="16" Height="11" EnableViewState="false" />
    </div>
    </form>
</body>
</html>

Display Rupee Symbol Website

To display the Rupee symbol on the website, just place this line in the <head> section and it will convert all the 'Rs' / 'Rs.' for you.

<script src="http://cdn.webrupee.com/js" type="text/javascript"></script>

Tuesday, October 12, 2010

Difference between ref & out

The out and the ref  parameters are very useful when your method needs to return more than one values.
The out Parameter
1. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
2. Variables passed as an out arguments need not be initialized prior to being passed.
class Example
{
    static void Method(out int i)
    {
        i = 90;
    }
    static void Main()
    {
        int k;
        Method(out k);
        // k is now 90
    }
}
3. The calling method is required to assign a value before the method returns.
class Example
{
    static void Method(out int i)
    {
        i = 90;
    }
    static void Main()
    {
        int k;
        Method(out k);  // k is now 90       
    }
}
The ref parameter
1. ref requires that the variable be initialized before being passed.
class Example
{
    static void Method(ref int i)
    {
        i += 30;
    }
    static void Main()
    {
        int k=3;
        Method(ref k);  // k is now 33       
    }
}

Note:
Methods cannot be overloaded if one method takes a ref argument and the other takes an out argument. These two methods, for example, are identical in terms of compilation, so this code will not compile:
class Example
{
    // compiler error CS0663: "cannot define overloaded
    // methods that differ only on ref and out"
    public void Method(out int i) {  }
    public void Method(ref int i) {  }
}
Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:
class Example
{
    public void Method(int i) {  }
    public void Method(out int i) {  }
}