Site Search:
Sign in | Join | Help
4Penny.net

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding

March 2007 - Posts

  • Setting the title on every page in a site

    To set the title of every page in an ASP.NET 2.0 web set:

     page.header.title = "my title"

     

  • Setting a Cookie

    Setting a cookie: 

                'save the new db to a cookie
                Dim newCookie As HttpCookie = New HttpCookie("intranet")
                newCookie("database") = strDatabase
                newCookie.Expires = Now.AddDays(10)
                Response.Cookies.Add(newCookie)

     Reading the cookie:

            'retrieve the database from a cookie
            If Not Request.Cookies("intranet") Is Nothing Then
                Session("database") = Request.Cookies("intranet")("database")
            Else
                Session("database") = "NGB01"
            End If

     

  • Validate an Email Address

    This piece of code will validate an email address.

     
    
    
    public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }

    Explanation:

    ^ marks the beginning of the sequence, $ marks the end:

    Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

    In between are 4 sequences:

    [\w-\.]+

    Brackets "[]" surround the range. "\w-" matches a word or a hyphed. "\." matches a period. So, "\w-\." matches words and hyphens followed by periods.  "+" matches one or more of the previous expressions.

    @

    This is the literal "@" symbol

    ([\w-]+\.)+

    Parens "()" are used to group. "[\w-]+" is one or more "words" or hyphens. "\." matches a literal period. The trailing "+" matches one or more of this group

     

    [\w-]{2,4}

    "[\w-]" matches a "word" or a hyphen. "{2,4}" allows 2-4 characters in this word