Friday, March 4, 2011

Maintain HTTP sessions of a web application in a centralized place

We create so many HTTP sessions and forget to kill them. Or, we create Http session object in different pages for the same functionality. They may or may not be having the same name. This creates so much of confusions and also leads to bugs (abnormal behavior) of the application if they are not properly  controlled or utilized.

The solution is to define any http session at one place (in the class) and use them in the most appropriate and wise manner.

In the APPCode, add a class with name SessionMgr.

public class SessionMgr
{
      public static string UserID
      {
          get
              {
                 return (string) HttpContext.Current.Session["UserId"];
               }
         set
              {
                HttpContext.Current.Session["UserId"]=value;
               }
       }
}

Like this as many session variables used in the project can be defined in one class and wherever, you need them call them by classname.variable name (SessionMgr.UserID).

In this way, you will always have control over the sessions in your project.

Thursday, March 3, 2011

Custom Event Handler

How to propogate any event for the control in the master page to all the child pages?
This can be achieved by Event Handling in .NET:
1. Create an event class that inherits the EventArgs class.
2. Create a Delegate for handling the event.
3. Declare an event in the master page.
4. Add the event to any of the method which raises the event.

Add the following Event Class in the APP Code folder in the website:
Event Class:
public class ABCCodeChangeEventArgs: EventArgs
{
                public ABCCodeChangeEventArgs(string toCode)
                {
                                _toCode = toCode;
                }
                private string _toCode;
                public string Code
                {
                                get { return _toCode;}
                                set{_toCode=value}
                }
}

public delegate void ABCCodeChangeEventHandler(object sender, ABCCodeChangeEventArgs e);

-------------------------------------------------------------------------------------------------------------
MasterPage.CS
Add the event to any control in the Master page:
public event ABCCodeChangeEventHandler BubbleABCCodeChangedEvent;
protected void ddlDropDown_SelectedIndexChanged(object Sender, EventArgs e)
{
                ABCCodeChangeEventArgs eventArgs = new
                ABCCodeChangeEventArgs(ddlDropdown.SelectedValue);
                BubbleABCCodeChangedEvent(this, eventArgs);
}
Add the following code in other pages in the project:
Page Load Event:
Master.BubbleABCCodeChangedEvent +=new ABCCodeChangeEventHandler(Master_BubbleABCCodeChangedEvent);
Define the method in the page:
void Master_BubbleABCCodeChangedEvent(object sender, ABCCodeChangeEventArgs e)
{
}

Common behavior for the aspx pages or common functionality for all the pages

How to make the aspx pages in your project to have common behavior or override the existing behavior of the all aspx pages?
1. Create a base class which inherits System.Web.UI.Page.
2. Write the logic to be implemented in the onload method or any other method where the behavior is expected to change.
3. Include the code to call the base method of the Page class.
4. Make all the aspx.cs classes to inherit the base class.
Basically this is useful when you want to check if a session is alive in the application or redirect the user to the login screen or to check the page permissions based on the role of the user.

public class BaseClass : System.Web.UI.Page
{
                protected override void OnLoad(EventArgs e)
                {
                                base.OnLoad(e);
                }
}