Thursday 26 April 2012

Smooth Alert Control using Jquery

Introduction
We often need to interact with the user at client side for different validation purposes or any other guideline purpose. For all these we prefer an easy, light and simple  techniques. Alerting user at client side must be very pretty because users on the Web are now in a habit of protectiveness in every thing that he face on the web.  I have developed a Message Box using jquery that is very easy to use and looks very pretty. It provides a simple way for developers to raise unobtrusive messages to the end-user. 

Background 

I named it braviMessageBox. This pop up message box  is a floating window that contains  a content area. The dialog window  closed with the 'x' icon by default.
If the content length exceeds the maximum height, a scrollbar will automatically appear. 

Using the code

braviMessageBox is very easy to use: You need to just add a refrence to Jquery and add the braviMessageBox.js file with the  braviMessageBox.css.

A quick code look "How to use" is here   

  <input type="button" id="btnTest" value="Click Me!" onclick="ShowMessage('Testing bravi Message Box..<br />this is a very pretty jquery message box control!');" /> 
Technique used in the js file is just create a DIV element and append it to page BODY.
function ShowMessage(message) {
    $("<div class='msg' id='dvMsg'><table width='100%'><tr><td width='90%'>" + message + "</td><td width='10%' valign='top' style='float:right;text-decoration:none;'><a href='javascript:void();' onclick='closethis(myMessage);'>x</a></td></tr></table></div>").attr('id', 'myMessage').appendTo('body');
    setTimeout("$('#myMessage').fadeOut('slow');", 2900);
    setTimeout("$('#myMessage').remove();", 4000);
} 

Though this message box will disappear after some time but still you can close it. There is the close method.


function closethis(id) {
    $('#myMessage').fadeOut('slow');
    setTimeout("$('#myMessage').remove();", 1000);
}
Now there isjust one css class to postion the Div in the Center of the screen. 
css class is:

 .msg
        {
            background-color: #4f7eaa;
            text-align: left;
            position: absolute;
            top: 35%;
            left: 40%;
            margin-left: -70px;
            width: 350px;
            height: auto;
            padding: 10px 10px 8px 10px;
            -moz-border-radius: 10px;
            -webkit-border-radius: 10px;
            -khtml-border-radius: 10px;
            border-radius: 10px;
        }

Future Programs 

This was my first article on a Forum. Now I aim to continue it and want to enhance this to a similar pop up dialog page.

Wednesday 25 April 2012

Folder Brows Dialog In ASP.Net


Introduction

ASP.Net does not provide a direct way to brows your computer's Directories. Active X can be used to achive this but its a bit tough for every client user to enable/install Active X on his/her computer.

After some research I created  a custom Folder Brows Control. Its not the same as we have in Win Forms Application by Microsoft.

To show the client computer's Local Drives I used a textbox to enter the value by the user, if the disk name is correct then it shows the directory structure else it will give error message.

Step1:
Create a Web page (Default.aspx) and paste the following code in the aspx page,  it should look like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FolderBrows_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
     <script type="text/javascript">
         function showDirectory() {
             showModalDialog("FolderBrows.aspx", 'Testing', "dialogHeight: 350px; dialogWidth: 360px; edge: Raised; center: Yes; help: Yes; resizable: No; status: No;");
             return false;
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="txtLocalFolder" runat="server" Width="340"></asp:TextBox>
                    <asp:Button OnClientClick="return showDirectory();" ID="btnBrowsLocal" runat="server"
                        Text="Browse" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



Step 2:

Add another web page (FolderBrows.aspx) (this will be the popup page) and it looks like:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FolderBrows.aspx.cs" Inherits="FolderBrows_FolderBrows" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <base target="_self"></base>
    <title>Borwse Directory</title>
    <style>
        .errorMsg
        {
            font-size: 8.25pt;
            color: red;
            font-family: Verdana, Arial;
            text-decoration: none;
        }
        .hilite
        {
            background-color: #dfe5ff;
        }
        .nohilite
        {
            background-color: #ffffff;
        }
        .text
        {
            font-size: 8.25pt;
            color: black;
            font-family: Verdana, Arial;
            text-decoration: none;
        }
        .tableOutlineWt
        {
            border-right: #cccccc 1px solid;
            border-top: #666666 1px solid;
            margin-top: 0px;
            overflow: auto;
            border-left: #333333 1px solid;
            padding-top: 0px;
            border-bottom: #cccccc 1px solid;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <script language="javascript">
        function SelectAndClose() {
            txtValue = document.getElementById('_browseTextBox').value;

            window.opener.document.getElementById("txtLocalFolder").value = txtValue;
            window.close();
            return false;
        }
    </script>
    <div>
        <table width="100%" border="0">
            <tr>
                <td>
                    <asp:Label ID="error" runat="server" CssClass="errorMsg"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Enter Drive Name &nbsp;
                    <asp:TextBox ID="txtDrive" AutoPostBack="true" runat="server" OnTextChanged="txtDrive_TextChanged"></asp:TextBox>
                    <div style="padding-left: 80px; font-size: smaller;">
                        [example:C, D, E, F]&nbsp;press &lt;ENTER&gt; to go!</div>
                    <asp:TextBox ID="_browseTextBox" runat="server" CssClass="toolbar" Width="300px"
                        Style="display: none" />
                </td>
            </tr>
            <tr>
                <td>
                    <div class="tableOutlineWt" style="width: 330px; height: 200px; background-color: white">
                        <table cellspacing="0" cellpadding="4" width="100%" bgcolor="#ffffff" border="0">
                            <tr>
                                <td>
                                    <asp:TreeView ID="TreeView1" runat="server" Height="190px" ImageSet="XPFileExplorer"
                                        NodeIndent="15" Width="300px">
                                        <ParentNodeStyle Font-Bold="False" />
                                        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
                                        <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
                                            VerticalPadding="0px" />
                                        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
                                            NodeSpacing="0px" VerticalPadding="2px" />
                                        <LeafNodeStyle ImageUrl="~/Images/folder.gif" />
                                    </asp:TreeView>
                                </td>
                            </tr>
                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Button ID="btnSelect" Text="OK" runat="server" Width="80" OnClientClick=" return SelectAndClose();" />&nbsp;
                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" Width="80" OnClientClick=" return  window.close();" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



Code Behind for FolderBrows.cs  

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Text.RegularExpressions;

public partial class SurveySubmissions_FolderBrows : System.Web.UI.Page
{
    TreeNode onjParent = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
        error.Text = "";
        TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
        TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
    }

    void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        _browseTextBox.Text = TreeView1.SelectedValue;
    }

    void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        //throw new Exception("The method or operation is not implemented.");
    }

    void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.Value.EndsWith("\\"))
        {
            AddNodes(e.Node.Value, e.Node);
        }


    }
    private TreeNode AddNodes(string path, TreeNode parentNode)
    {
        FileList objList = new FileList(path, "*.*");
        TreeNode node = new TreeNode(path, path);
        for (int index = 0; index < objList.Directories.Length; index++)
        {
            string directory = objList.Directories[index];
            TreeNode objChildNode = new TreeNode(directory, path + "\\" + directory + "\\");
            objChildNode.PopulateOnDemand = true;
            objChildNode.Target = "_blank";

            parentNode.ChildNodes.Add(objChildNode);
            HasChild = true;
        }
        //do not show files
        ////////////////////
        //foreach (string file in objList.Files)
        //{
        //    TreeNode objChildNode = new TreeNode(file, path + "\\" + file);
        //    parentNode.ChildNodes.Add(objChildNode);
        //}
        return node;
    }
    bool HasChild = false;
    protected void txtDrive_TextChanged(object sender, EventArgs e)
    {
        Regex r = new Regex("^[A-Z]");
        Match m = r.Match(txtDrive.Text.ToUpper());
        if (m.Success)
        {
            TreeView1.Nodes.Clear();
            onjParent = new TreeNode(txtDrive.Text + @":\");

            onjParent.PopulateOnDemand = true;
            TreeView1.Nodes.Add(onjParent);

            AddNodes(TreeView1.Nodes[0].Value, onjParent);

            if (!HasChild)
            {
                error.Text = "Invalid Drive Entered!";
                TreeView1.Nodes.Clear();
                return;
            }
            TreeView1.CollapseAll();
        }
        else
            error.Text = "Your input is invalid!";
    }
}



Step 3:

Add Node method in the  FolderBrows.aspx which add the tree node of the directory and files. FileList Constructor takes path and the filters for the files.
Add the following two classes in your project

1:-FileList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Net;
using System.IO;

/// <summary>
/// Summary description for FileList
/// </summary>
public class FileList
{
    protected string[] _directories = new string[0];
    protected string[] _files = new string[0];
    protected string _basedirectory = null;
    protected const string httpTag = "http://";

    /// <summary>
    /// Path to the directory that is being browsed
    /// </summary>
    public string BaseDirectory
    {
        get { return _basedirectory; }
    }

    /// <summary>
    /// Array of the directories that were found
    /// </summary>
    public string[] Directories
    {
        get { return _directories; }
    }

    /// <summary>
    /// Array of the files that matched the specified filter(s)
    /// </summary>
    public string[] Files
    {
        get { return _files; }
    }

    public static string[] ExtractHrefsFromPage(byte[] pagedata)
    {
        string htmlpage = ASCIIEncoding.ASCII.GetString(pagedata);
        Regex hrefparser = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))",
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        ArrayList results = new ArrayList();
        for (Match found = hrefparser.Match(htmlpage); found.Success; found = found.NextMatch())
        {
            results.Add(HttpUtility.UrlDecode(found.Groups[1].Value));
        }

        string[] hrefs = new string[results.Count];
        results.CopyTo(hrefs);
        return hrefs;
    }

    protected string ExtractUrifromUrl(string urlstring)
    {
        int delimiter = urlstring.IndexOf("/", httpTag.Length);
        if (-1 == delimiter)
            return "/";

        return urlstring.Substring(delimiter);
    }

    static public string GetPath(string file)
    {
        int delimiter = file.LastIndexOfAny(new char[] { '\\', '/' });
        return file.Substring(0, delimiter + 1);
    }

    protected static string GetContentType(string url)
    {
        HttpWebRequest headRequest = WebRequest.Create(url) as HttpWebRequest;
        headRequest.Method = "HEAD";

        HttpWebResponse response = headRequest.GetResponse() as HttpWebResponse;
        return response.ContentType;
    }

    public static bool IsHTMLContent(string url)
    {
        string content = GetContentType(url);

        if (null == content || -1 == content.ToLower().IndexOf("html"))
            return false;

        return true;
    }

    protected void ParseHTTPPage(string directory, string filter)
    {
        try
        {
            string[] filterlist = filter.Split(';');
            if (!FileList.IsHTMLContent(directory))
                directory = GetPath(directory);

            string uristring = ExtractUrifromUrl(directory);

            WebClient client = new WebClient();
            byte[] pagedata = client.DownloadData(directory);
            string[] hrefs = ExtractHrefsFromPage(pagedata);

            ArrayList dirs = new ArrayList();
            ArrayList files = new ArrayList();
            foreach (string uri in hrefs)
            {
                if (uri.EndsWith("/"))
                {
                    //  handle the directory
                    if (uri.StartsWith(uristring))
                        dirs.Add(uri.Substring(uristring.Length).Trim('/'));
                }
                else
                {
                    string file = Path.GetFileName(uri);
                    foreach (string query in filterlist)
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(file, "." + query.Replace(".", "\\."), RegexOptions.IgnoreCase))
                        {
                            files.Add(file);
                            break;
                        }
                    }
                }
            }

            _directories = new string[dirs.Count];
            dirs.CopyTo(_directories);
            System.Array.Sort(_directories);

            _files = new string[files.Count];
            files.CopyTo(_files);
            System.Array.Sort(_files);

            _basedirectory = directory;
            if (!_basedirectory.EndsWith("/"))
                _basedirectory += "/";
        }
        catch (Exception except)
        {
            System.Diagnostics.Trace.WriteLine("Exception parsing URL: " + except.Message);
        }
        return;
    }

    protected void PasreUNCPage(string directory, string filter)
    {
        try
        {
            if (FileAttributes.Directory != (FileAttributes.Directory & File.GetAttributes(directory)))
                directory = GetPath(directory);
        }
        catch (Exception)
        {
            return;
        }

        try
        {
            _directories = RelativePaths(Directory.GetDirectories(directory, "*.*"));
            System.Array.Sort(_directories);
        }
        catch (Exception except)
        {
            System.Diagnostics.Trace.WriteLine("Exception parsing directory: " + except.Message);
        }

        try
        {
            _files = new string[0];
            string[] extensions = filter.Split(';');
            foreach (string ext in extensions)
            {
                string[] foundfiles = RelativePaths(Directory.GetFiles(directory, ext));
                if (foundfiles.Length > 0)
                {
                    string[] newlist = new string[_files.Length + foundfiles.Length];
                    _files.CopyTo(newlist, 0);
                    foundfiles.CopyTo(newlist, _files.Length);
                    _files = newlist;
                    System.Array.Sort(_files);
                }
            }
        }
        catch (Exception except)
        {
            System.Diagnostics.Trace.WriteLine("Exception parsing files: " + except.Message);
        }

        _basedirectory = directory;
        if (!_basedirectory.EndsWith("\\"))
            _basedirectory += "\\";
        return;
    }

    public FileList(string directory, string filter)
    {
        if (null == directory || 0 == directory.Length)
        {
            _files = new string[0];
            _directories = new string[0];
            _basedirectory = "";
            return;
        }

        if (PathHelper.IsHTTPPath(directory))
        {
            ParseHTTPPage(directory, filter);
        }
        else
        {
            PasreUNCPage(directory, filter);
        }
        return;
    }

    protected string[] RelativePaths(string[] filelist)
    {
        if (null == filelist)
            return new string[0];

        string[] result = new string[filelist.Length];
        for (int index = 0; index < filelist.Length; index++)
            result[index] = Path.GetFileName(filelist[index]);

        return result;
    }
}


2:-PathHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PathHelper
/// </summary>

public class PathHelper
{
    protected const string httpTag = "http://";
    protected const int minUNCBackSlashCount = 4;     //    The UNC path "\\server\path\" has 4 back slashes

    static public string GetParentPath(string path)
    {
        int delimiter = 0;
        string parent = "";
        string current = path.TrimEnd(new char[] { '\\', '/' });
        if (IsHTTPPath(current))
        {
            delimiter = current.LastIndexOfAny(new char[] { '/' });
            if (-1 == delimiter)
                return path;

            parent = current.Substring(0, delimiter + 1);
            if (parent.Length < httpTag.Length || httpTag == parent.ToLower())
                return current + '/';

            return parent;
        }

        //  UNC Path handling
        delimiter = current.LastIndexOfAny(new char[] { '\\' });
        if (-1 == delimiter)
            return path;

        parent = current.Substring(0, delimiter + 1);
        string[] parts = parent.Split('\\');
        if (parts.Length <= minUNCBackSlashCount)
            return current + '\\';

        return parent;
    }

    public static bool IsHTTPPath(string directory)
    {
        if (null == directory || directory.Length < httpTag.Length)
            return false;

        return httpTag == directory.Substring(0, httpTag.Length).ToLower();
    }
}


Screen Shot:



Now when you select a folder from the tree view  and click OK then it will show into the parent page' text box.
Happy coding....