06 September 2010

IntelliSense & Type Safety for Session Variables

Keeping track of your Session variables in ASP.NET can become a bit cumbersome task if you end up creating many variables across a large Web Application. Sometimes you might make common mistakes like misspelling the session variable name or at times end up with incorrect data types.

I have found a very good solution for this problem which i use regularly in my ASP.NET applications. The solution is to use an "SessionHandler" class in your Application in which one creates properties to access the session variables. By this method we not only are able to access the Session variables through Visual Studio IntelliSense but also apply type safety to them.

Here is an quick example of this class:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/*
 *
 File Name : SessionHandler.cs
 Class Name: SessionHandler
 Summary   : This class is used to handle the session variables and keep a track of them using the
             SessionHandler class object.
             Advantage is that these variables are shown in VS Intellisense.
             And we can also implement type safety.
 *
 */
public static class SessionHandler
{
    ///////////////////////////////////////////////////////////////////////////////////////////

    private static string _UserKey = "UserKey";
    private static string _Username = "Username";  
    private static string _LoginType = "LoginType";
    
       

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string UserKey
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._UserKey];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._UserKey] = value;
        }
    }

  

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string LoginType
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._LoginType];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._LoginType] = value;
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string Username
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._Username];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._Username] = value;
        }
    }

}

Please Note: You need to add this class file to your AppCode folder in your Web Application.

06 June 2010

Working On Windows Application

Web to Windows Application shift was not something i was hoping for when i stared with ASP.NET.
But as it turns around i got a Good Domain "Medical Insurance" to work on, and so i readily accepted the challange.
Been busy eversince last 4 months.
Hey it does get boring at times but just look at the good things i got to learn...

1.Large database handling techniques!!
2.Really improved my SQL Server knowledge and skills.
3.Learnt a lot more about Crystal Reports and Windows Application.
4.Got to know how to communicate with clients (USA). And loving it!
5.Really learnt how to work in a shared environment, using source safe, communicating with developers across the globe, coding standards etc etc.

So all in all its a great addition to my Resume!

And now the Best Part is that we are implementing the same functionality on a Web Portal as well.
Ain't that great!!

ASP.NET here i come again!!

01 April 2010

Web 2.0 pop-up dialog using Ajax Control Toolkit

Tired of using the old boring Windows Xp style JavaScript pop-ups to get user confirmation on a web page?

 Then you have come to the right place cause i will just show you how easy it is to create a customized Vista type confirm dialog using the Ajax Control Toolkit and ASP.NET 2.0 or greater.

There's absolutely no need to know any hard core JavaScript here !!

So here are the steps:


Step 1: Install the Ajax Control Toolkit on your machine if  already not done. Start your web application and add the Ajax Control Toolkit dll to your bin folder.


Step 2: Include the following  the reference on your page

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


Alternative if you have configured your Toolbox then simply ignore the step 1,2 and go directly to step 3.


Step 3: Now you would require to add a asp:Panel on your page that will hold the HTML required for the popup screen.  ex:


<asp:Panel ID="PanelDeleteAlert" runat="server" CssClass="confirmBox"
Style="display: none;width: 300px;">
           <div id="confirmHeader">
               <span>Confirm</span></div>
           <div id="confirmMsg" align="center">
               <div class="rowdiv">
                   <div class="celldiv">
                       <img src="../images/confirm.gif" alt="" /></div>
                   <div class="celldiv">
                       <span>Delete<asp:Label ID="lblDeleteName"
runat="server" Font-Bold="true" Text="Do you want to delete this?"></asp:Label>
                           from contact list?</span></div>
               </div>
           </div>
           <div id="confirmButtons">
               <asp:Button ID="ButtonOk2" runat="server" Text="OK"
CssClass="button" />
               <asp:Button ID="ButtonCancel2" runat="server"
Text="Cancel" CssClass="button" />
           </div>
</asp:Panel>


Step 4: Now you need a LinkButton/Button on who's click you need to show the pop-up, an ConfirmButtonExtender & an ModalPopupExtenderfrom from Toolkit. Configure these as follows:

<div style="float: right">
       <asp:LinkButton ID="butDelete" OnClick="butDelete_Click"
runat="server" CssClass="linktext"  Text="Delete Contact"></asp:LinkButton>
       <cc1:ConfirmButtonExtender ID="ConfirmButtonExtender2"
runat="server" TargetControlID="butDelete"  ConfirmText="Do you want to delete this
contact?" DisplayModalPopupID="ModalPopupExtender2" Enabled="True">
                   </cc1:ConfirmButtonExtender>
       <cc1:ModalPopupExtender ID="ModalPopupExtender2"
runat="server" TargetControlID="butDelete"  PopupControlID="PanelDeleteAlert"
OkControlID="ButtonOk2" CancelControlID="ButtonCancel2" BackgroundCssClass="modalBackground" />
</div>

Step 5: You need to create proper CSS for the Panel and  for the background when the Pop-up is open. You can use your imagination for designing the Panel and for the Background you can use the following CSS

/*Modal Popup*/

.modalBackground {
    background-color:Gray;

    filter:alpha(opacity=70);
    opacity:0.7;

}


.modalPopup {

    background-color:#ffffdd;

    border-width:3px;

    border-style:solid;

    border-color:Gray;

    padding:3px;

    width:250px;

}

Step 6: That's it go ahead and test your application


So as you can see using your own imagination and some good CSS techniques , you can come up with a really neat looking confirmation dialog!!