Thursday, November 27, 2008

 

ASP.NET Chart Control

I just downloaded and installed ASP.NET Chart Controls that ScottGu just announced.

I'm looking at the sample documentation and it looks very detailed because one of the problems I had with the Telerik and ComponentOne Chart controls that I have used in the past is the poor documentation, but Microsoft ASP.NET Charts docs seem to go a step further and explain every nitty-gritty of the charts along with C# and VB.NET source code, I'm looking forward to try them soon.

Labels: ,


Tuesday, November 25, 2008

 

Using CustomValidator to compare Time Values

ASP.NET ships with many client-side validation controls out of the box, one of them is the Custom Validator control. In client input scenario if you need to compare two date values you can use a CompareValidator and set its Type property to Date, for example check this link. However anytime you need to compare two Time values then there is no support for this in any validation controls.This is how Telerik's TimePicker control looks like.

 

ScreenHunter_02 Nov. 25 12.57

Fortunately comparing the two values is easy using the CustomValidator control and a little bit of javascript. The CustomValidator control has a ClientValidationFunction property that we can use to trigger a javascript function and using the following function we can can compare two time values.

<script type="text/javascript">
    function compareTime(sender, args) {
        var start = document.getElementById("<%=rdpStarttime.ClientID %>");
        var end = document.getElementById("<%=rdpEndtime.ClientID %>");
        var starttime = new Date(0, 0, 0, start.value.substring(11, 13), start.value.substring(14, 16));
        var endtime = new Date(0, 0, 0, end.value.substring(11, 13), end.value.substring(14, 16));
        args.IsValid = (endtime >= starttime);
    }
</script>


<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="End time should be greater than Start" ClientValidationFunction="compareTime" ControlToValidate="rdpEndtime"></asp:CustomValidator>
 
 

Here's how it looks like.


 ScreenHunter_03 Nov. 25 12.16

Labels: , ,


Sunday, November 23, 2008

 

C# Extension methods library

Extension methods are a quick way to extend the types available in the CLR such as string or object and add your own methods without having to re-compile your application. If you are not familiar with Extension methods then check out ScottGu's post.

A while back I brought back an old C# class that had a collection of static 'helper methods' that we used to use back in .NET 1.1 days and decided to port it to .NET 2.0 as Extension methods.

Fortunately doing that was too simple, in fact it was just a matter of adding a this keyword to a method parameter and I had a strongly typed Extension methods class ready.

While there are dozens of Extension method libraries available at CodePlex.com but I decided to stick to my own library for now that is available for download - Extensions.cs or get the code below.

using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Data;
namespace Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Check for Positive Integers with zero inclusive  
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static string HtmlEncode(this string String)
        {
            if (String == "")
                return "";
            
            return HttpUtility.HtmlEncode(String);
        }
 
        /// <summary>
        /// Check for Positive Integers with zero inclusive  
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static string HtmlDecode(this string String)
        {
            if (String == "")
                return "";
 
            return HttpUtility.HtmlDecode(String);
        }
        /// <summary>
        /// Check for Positive Integers with zero inclusive  
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsWholeNumber(this string strNumber)
        {
            if (strNumber == "")
                return false;
 
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strNumber);
        }
 
        /// <summary>
        /// Check if the string is Double  
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsDouble(this string strNumber)
        {
            if (strNumber == "")
                return false;
 
            try
            {
                Convert.ToDouble(strNumber);
                
            }
            catch (Exception)
            {
 
                return false;
            }
 
            return true;
 
        }
 
        /// <summary>
        ///Function to Check for AlphaNumeric. 
        /// </summary>
        /// <param name="strToCheck"> String to check for alphanumeric</param>
        /// <returns>True if it is Alphanumeric</returns>
        public static bool IsAlphaNumeric(this string strToCheck)
        {
            bool valid = true;
 
            if (strToCheck == "")
                return false;
 
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
 
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);
            return valid;
        }
 
        /// <summary>
        ///Function to Check for valid alphanumeric input with space chars also
        /// </summary>
        /// <param name="strToCheck"> String to check for alphanumeric</param>
        /// <returns>True if it is Alphanumeric</returns>
        public static bool IsValidAlphaNumericWithSpace(this string strToCheck)
        {
            bool valid = true;
 
            if (strToCheck == "")
                return false;
 
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9\\s]");
 
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);
            return valid;
        }
 
        /// <summary>
        /// Check for valid alphabet input with space chars also
        /// </summary>
        /// <param name="strToCheck"> String to check for alphanumeric</param>
        /// <returns>True if it is Alphanumeric</returns>
        public static bool IsValidAlphabetWithSpace(this string strToCheck)
        {
            bool valid = true;
 
            if (strToCheck == "")
                return false;
 
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\s]");
 
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);
            return valid;
        }
 
        /// <summary>
        /// Check for valid alphabet input with space chars also
        /// </summary>
        /// <param name="strToCheck"> String to check for alphanumeric</param>
        /// <returns>True if it is Alphanumeric</returns>
        public static bool IsValidAlphabetWithHyphen(this string strToCheck)
        {
            bool valid = true;
 
            if (strToCheck == "")
                return false;
 
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\-]");
 
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);
            return valid;
        }
 
        /// <summary>
        ///  Check for Alphabets.
        /// </summary>
        /// <param name="strToCheck">Input string to check for validity</param>
        /// <returns>True if valid alphabetic string, False otherwise</returns>
        public static bool IsAlpha(this string strToCheck)
        {
            bool valid = true;
 
            if (strToCheck == "")
                return false;
 
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
 
            valid = !objAlphaPattern.IsMatch(strToCheck);
            return valid;
        }
        
        /// <summary>
        /// Check whether the string is valid number or not
        /// </summary>
        /// <param name="strNumber">Number to check for </param>
        /// <returns>True if valid number, False otherwise</returns>
        public static bool IsNumber(this string strNumber)
        {
            try
            {
                Convert.ToDouble(strNumber);
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strInteger"></param>
        /// <returns></returns>
        public static bool IsInteger(this string strInteger)
        {
            try
            {
                if (string.IsNullOrEmpty(strInteger))
                    return false;
 
                Convert.ToInt32(strInteger);
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strDateTime"></param>
        /// <returns></returns>
        public static bool IsDateTime(this string strDateTime)
        {
            try
            {
                Convert.ToDateTime(strDateTime);
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// Function to validate given string for HTML Injection
        /// </summary>
        /// <param name="strBuff">String to be validated</param>
        /// <returns>Boolean value indicating if given input string passes HTML Injection validation</returns>
        public static bool IsValidHTMLInjection(this string strBuff)
        {
            return (!Regex.IsMatch(HttpUtility.HtmlDecode(strBuff), "<(.|\n)+?>"));
        }
 
        /// <summary>
        /// Checks whether a valid Email address was input
        /// </summary>
        /// <param name="inputEmail">Email address to validate</param>
        /// <returns>True if valid, False otherwise</returns>
        public static bool isEmail(this string inputEmail)
        {
 
            if (inputEmail != null && inputEmail != "")
            {
                string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                    @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                    @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                Regex re = new Regex(strRegex);
                if (re.IsMatch(inputEmail))
                    return (true);
                else
                    return (false);
            }
            else
                return (false);
        }
 
        /// <summary>
        /// Converts a valid string to integer
        /// </summary>
        /// <param name="StringToConvert"></param>
        /// <returns></returns>
        public static int ToInteger(this string StringToConvert)
        {
            try
            {
                return Convert.ToInt32(StringToConvert.ToString());
            }
            catch
            {
                throw new Exception("String could not be converted to Integer");
            }
        }
 
        /// <summary>
        /// Converts an Object to it's integer value
        /// </summary>
        /// <param name="ObjectToConvert"></param>
        /// <returns></returns>
        public static int ToInteger(this object ObjectToConvert)
        {
            try
            {
                return Convert.ToInt32(ObjectToConvert.ToString());
            }
            catch
            {
                throw new Exception("Object cannot be converted to Integer");
            }
        }
 
        /// <summary>
        /// Converts an Object to it's integer value
        /// </summary>
        /// <param name="ObjectToConvert"></param>
        /// <returns></returns>
        public static long ToLong(this object ObjectToConvert)
        {
            try
            {
                return Convert.ToInt64(ObjectToConvert.ToString());
            }
            catch
            {
                throw new Exception("Object cannot be converted to Long");
            }
        }
 
        /// <summary>
        /// Converts a valid string to double
        /// </summary>
        /// <param name="StringToConvert"></param>
        /// <returns></returns>
        public static double ToDouble(this string StringToConvert)
        {
            return Convert.ToDouble(StringToConvert);
        }
 
        /// <summary>
        /// Converts an Object to it's double value
        /// </summary>
        /// <param name="ObjectToConvert"></param>
        /// <returns></returns>
        public static double ToDouble(this object ObjectToConvert)
        {
            try
            {
                return Convert.ToDouble(ObjectToConvert.ToString());
            }
            catch
            {
                throw new Exception("Object cannot be converted to double");
            }
        }
 
        /// <summary>
        /// Converts an Object to it's decimal value
        /// </summary>
        /// <param name="ObjectToConvert"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object ObjectToConvert)
        {
            try
            {
                return Convert.ToDecimal(ObjectToConvert.ToString());
            }
            catch
            {
                throw new Exception("Object cannot be converted to decimal");
            }
        }
 
        /// <summary>
        /// Converts an String to it's decimal value
        /// </summary>
        /// <param name="ObjectToConvert"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string StringToConvert)
        {
            try
            {
                return Convert.ToDecimal(StringToConvert.ToString());
            }
            catch
            {
                throw new Exception("String cannot be converted to decimal");
            }
        }
 
        /// <summary>
        /// Converts a string to a Sentence case
        /// </summary>
        /// <param name="String"></param>
        /// <returns></returns>
        public static string ToSentence(this string String)
        {
            if (String.Length > 0)
                return String.Substring(0, 1).ToUpper() + String.Substring(1,String.Length - 1);
 
            return "";
        }
 
        public static bool ToBool(this object Object)
        {
            try
            {
                return Convert.ToBoolean(Object.ToString());
            }
            catch {
                throw new Exception("Object cannot be converted to Boolean");
            }
 
        }
 
        public static DateTime ToDateTime(this string String)
        {
            try
            {
                return Convert.ToDateTime(String);
            }
            catch (Exception ex)
            {
                throw new Exception("Object cannot be converted to DateTime. Object: " + String);
            }
 
        }
 
        public static DateTime ToDateTime(this object Object)
        {
            try
            {
                return Convert.ToDateTime(Convert.ToString(Object));
            }
            catch (Exception ex)
            {
                throw new Exception("Object cannot be converted to DateTime. Object: " + Object);
            }
 
        }
 
        /// <summary>
        /// Selects specific number of rows from a datatable
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static DataTable SelectRows(this DataTable dataTable, int rowCount)
        {
            try
            {
                DataTable myTable = dataTable.Clone();
                DataRow[] myRows = dataTable.Select();
                for (int i = 0; i < rowCount; i++)
                {
                    if (i < myRows.Length)
                    {
                        myTable.ImportRow(myRows[i]);
                        myTable.AcceptChanges();
                    }
                }
 
                return myTable;
 
            }
            catch (Exception)
            {
                return new DataTable();
            }
        }
 
        /// <summary>
        /// Accepts a date time value, calculates number of days, minutes or seconds and shows 'pretty dates'
        /// like '2 days ago', '1 week ago' or '10 minutes ago'
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string GetPrettyDate(this DateTime d)
        {
            // 1.
            // Get time span elapsed since the date.
            TimeSpan s = DateTime.Now.Subtract(d);
 
            // 2.
            // Get total number of days elapsed.
            int dayDiff = (int)s.TotalDays;
 
            // 3.
            // Get total number of seconds elapsed.
            int secDiff = (int)s.TotalSeconds;
 
            // 4.
            // Don't allow out of range values.
            if (dayDiff < 0 || dayDiff >= 31)
            {
                return d.ToString();
            }
 
            // 5.
            // Handle same-day times.
            if (dayDiff == 0)
            {
                // A.
                // Less than one minute ago.
                if (secDiff < 60)
                {
                    return "just now";
                }
                // B.
                // Less than 2 minutes ago.
                if (secDiff < 120)
                {
                    return "1 minute ago";
                }
                // C.
                // Less than one hour ago.
                if (secDiff < 3600)
                {
                    return string.Format("{0} minutes ago",
                        Math.Floor((double)secDiff / 60));
                }
                // D.
                // Less than 2 hours ago.
                if (secDiff < 7200)
                {
                    return "1 hour ago";
                }
                // E.
                // Less than one day ago.
                if (secDiff < 86400)
                {
                    return string.Format("{0} hours ago",
                        Math.Floor((double)secDiff / 3600));
                }
            }
            // 6.
            // Handle previous days.
            if (dayDiff == 1)
            {
                return "yesterday";
            }
            if (dayDiff < 7)
            {
                return string.Format("{0} days ago",
                    dayDiff);
            }
            if (dayDiff < 31)
            {
                return string.Format("{0} weeks ago",
                    Math.Ceiling((double)dayDiff / 7));
            }
            return null;
        }
 
        /// <summary>
        /// This mehtod will extend the response mehtod and open the page in a new window
        /// </summary>
        /// <param name="response"></param>
        /// <param name="url"></param>
        /// <param name="target"></param>
        /// <param name="windowFeatures"></param>
        public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)
        {
            if ((String.IsNullOrEmpty(target) ||
                target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
                String.IsNullOrEmpty(windowFeatures))
            {
                response.Redirect(url);
            }
            else
            {
                Page page = (Page)HttpContext.Current.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException(
                        "Cannot redirect to new window outside Page context.");
                }
 
                url = page.ResolveClientUrl(url);
                string script;
                if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
                }
                else
                {
                    script = @"window.open(""{0}"", ""{1}"");";
                }
                script = String.Format(script, url, target, windowFeatures);
 
                ScriptManager.RegisterStartupScript(page,
                    typeof(Page),
                    "Redirect",
                    script,
                    true);
            }
        }
 
 
    }
}

Extensions


Update: Feb 5, 2010 – Fix broken download, new extension methods

Labels: , , , ,


This page is powered by Blogger. Isn't yours?