Blog posts


  • Prevent the web browser using the cache for your ASP.NET pages

    September 18, 2009 | Tags: ASP.NET, C#, Snippet
    Response.AddHeader( "Cache-Control","no-cache");
    Response.Expires = 0;
    Response.Cache.SetNoStore();
    Response.AddHeader("Pragma", "no-cache");

  • jQuery: Only allow user to enter numbers

    September 18, 2009 | Tags: Javascript, jQuery, Snippet
    Here's a nice little snippet for blocking all other key presses than numerical and backspace:
    $('#selector').bind('keypress', function(e) {
    return !( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
    }

    (#selector is the selector to your input field/fields to apply this filter. For info on how selectors works in jQuery please visit the official documentation about selectors.)

  • C#: Remove all non numeric characters from a string using Regex

    September 15, 2009 | Tags: C#, Regular expressions, Snippet
    using System.Text.RegularExpressions;
    ...
    string newString = Regex.Replace(oldString, "[^.0-9]", "");
    (If you don't want to allow the decimal delimiter in the final result, remove the . from the regular expression above).

  • Url encoding in C#

    September 15, 2009 | Tags: ASP.NET, C#, Snippet
    string encoded = System.Web.HttpUtility.UrlEncode("string to encode");
    If you're planning to do this from a win form application, be sure to add System.Web as a reference.

  • ASP.NET: Event order

    September 11, 2009 | Tags: ASP.NET, Troubleshooting
    So much troubleshooting of ASP.NET is aided by understanding the order in which the events occur within the page life cycle. For example, notice how the Init and Load events are inverted for Pages the ChildUserControls.

    Here you'll find all the phases that an ASP.NET-page goes through.

    BeginRequest - HttpModule
    AuthenticateRequest - HttpModule
    PostAuthenticateRequest - HttpModule
    PostAuthorizeRequest - HttpModule
    ResolveRequestCache - HttpModule
    PostResolveRequestCache - HttpModule
    PostMapRequestHandler - HttpModule
    AcquireRequestState - HttpModule
    PostAcquireRequestState - HttpModule
    PreRequestHandlerExecute - HttpModule

    PreInit - Page

    Init - ChildUserControl
    Init - UserControl
    Init - MasterPage
    Init - Page

    InitComplete - Page

    LoadPageStateFromPersistenceMedium - Page

    ProcessPostData (first try) - Page

    PreLoad - Page

    Load - Page
    Load - MasterPage
    Load - UserControl
    Load - ChildUserControl

    ProcessPostData (second try) - Page

    RaiseChangedEvents - Page
    RaisePostBackEvent - Page

    Click - Button - ChildUserControl

    DataBinding - Page
    DataBinding - MasterPage
    DataBinding - UserControl
    DataBinding - ChildUserControl

    LoadComplete - Page

    PreRender - Page
    PreRender - MasterPage
    PreRender - UserControl
    PreRender - ChildUserControl

    PreRenderComplete - Page

    SaveViewState - Page
    SavePageStateToPersistenceMedium - Page
    SaveStateComplete - Page

    Unload - ChildUserControl
    Unload - UserControl
    Unload - MasterPage
    Unload - Page

    PostRequestHandlerExecute - HttpModule
    ReleaseRequestState - HttpModule
    PostReleaseRequestState - HttpModule
    UpdateRequestCache - HttpModule
    PostUpdateRequestCache - HttpModule
    EndRequest - HttpModule
    PreSendRequestHeaders - HttpModule
    PreSendRequestContent - HttpModule