Tuesday, October 18, 2011

Navigate to Previous Page in ASP.NET

 
Using Request.UrlReferrer, we can get the Previous page URL of the current
 request.Previous Page information is available in the Request.UrlReferrer
 property only if user is redirected to the current page from some other
 page and is available in the !Page.IsPostBack of Page Load event of the form.
 Here we use ViewState because it can be used if you want to store the values 
on postback to the same form. 
ViewState is used for retaining values between multiple requests for the same
 page
This is the default method that the page uses to preserve page and control property 
values between round trips.  
 
... Default Page... 
<asp:Button runat="server" ID="btnBack" Text="Back" onclick="btnBack_Click" />
 
 
----------------------.cs
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
//check if the webpage is loaded for the first time. 
{
            ViewState["PreviousPage"] = 
  Request.UrlReferrer;//Saves the Previous page url in ViewState 
}
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        if (ViewState["PreviousPage"] != null) //Check if the ViewState  
//contains Previous page URL        {
            Response.Redirect(ViewState["PreviousPage"].ToString());
//Redirect to
   //Previous page by retrieving the PreviousPage Url from ViewState. 
}
    } 

Monday, October 17, 2011

Watermark TextBox using JavaScript

<script type = "text/javascript">
    var defaultText = "Enter your text here";
    function WaterMark(txt, evt)
    {
        if(txt.value.length == 0 && evt.type == "blur")
        {
            txt.style.color = "gray";
            txt.value = defaultText;
        }
        if(txt.value == defaultText && evt.type == "focus")
        {
            txt.style.color = "black";
            txt.value="";
        }
    }
</script>


---------------------------
<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"
    ForeColor = "Gray" onblur = "WaterMark(this, event);"
    onfocus = "WaterMark(this, event);">
</asp:TextBox>



---------------------------OR-----------------------
TextBox1.Attributes.Add("onblur", "WaterMark(this, event);");
TextBox1.Attributes.Add("onfocus", "WaterMark(this, event);");  

Thursday, October 13, 2011

Date Formatting in C# with region and sql compatible

 public string myDateFormat(string p)
    {
        // throw new NotImplementedException();

        IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
        String datetime = p.ToString();
        DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
        return dt.ToString();
    }

Wednesday, October 12, 2011

Static Classes and Static Class Members (C# Programming Guide)

http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.80%29.aspx

Static Constructors (C# Programming Guide)

The following list provides the main features of a static class:
Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization. For more information, see Static Constructors (C# Programming Guide).
-----------------------------------------------------------------------



A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
It is more typical to declare a non-static class with some static members, than to declare an entire class as static. Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object instance is required.
C# does not support static local variables (variables that are declared in method scope).
You declare static class members by using the static keyword before the return type of the member, as shown in the following example:
public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    // Other non-static fields and properties...
}

Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member, as shown in the following example:
Automobile.Drive();
int i = Automobile.NumberOfWheels;

If your class contains static fields, provide a static constructor that initializes them when the class is loaded.
A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant.

Monday, October 10, 2011

Javascript Trim Member Functions


function trim(stringToTrim) {
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
 return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
 return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");
 
 
----------------------------------------------------
 

function ltrim(str) { 
 for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
 return str.substring(k, str.length);
}
function rtrim(str) {
 for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
 return str.substring(0,j+1);
}
function trim(str) {
 return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
 var whitespaceChars = " \t\n\r\f";
 return (whitespaceChars.indexOf(charToCheck) != -1);
}