Search Code Shode

Wednesday, January 19, 2011

Enlarge Image on Mouseover in ASP.NET using jQuery

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
    <style type="text/css">
            height: 100px;
            width: 100px;
            position: relative;
            position: relative;
            width: 400px;
            height: 250px;
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $(".thumbnail").mouseover(function () {
                $(".thumbnail").css("opacity", ".5");
                $(this).animate({ opacity: 1.0 });
                $("#imgContainer").append("<img class='image' src='" + $(this).attr("src") + "' />");

            $(".thumbnail").mouseout(function () {
                $(".thumbnail").css("opacity", "1.0");
    <form id="form1" runat="server">
    <div align="center">
        <img alt="" class="thumbnail" src="Images/Tree.jpg" />
        <img class="thumbnail" src="Images/dock.jpg" alt="" />
        <img class="thumbnail" src="Images/forest.jpg" alt="" />
        <img class="thumbnail" src="Images/garden.jpg" alt="" />
        <div id="imgContainer">

Sunday, January 9, 2011

Weeks Calculation C# VB.NET

For some reason, Microsoft didn’t add a Week property to the DateTime class. I never could figure out why. Instead they gave us the System.Globalization namespace, filled with date related functionality like the different calendar classes. In this example, I’m going to use the GregorianCalendar class to find the week number of a certain date.

using System.Globalization;
public static int WeekNumber(DateTime date)
   GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
   return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

As you can see, it’s not that difficult as long as you know where to look.
You can also check for the total number of weeks in a certain year. You would have to know the last day of the year, that never can be part of the first week of the following year. That day is december 28th. Here is a method that gives you the number of weeks in a specified year.
using System.Globalization;
public static int WeeksInYear(int year)
   GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
   return cal.GetWeekOfYear(new DateTime(year, 12, 28), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

Now for the tricky part. Let’s say you want to know the number of weeks between two dates. Just pass the two dates into the above method and subtract them from each other like “WeekNumber(dateFrom) – WeekNumber(dateTo)  + 1”. Wrong! If the first date is from another year than the second one, it will not work.
In order to write a method that takes two dates and return the number of weeks in between them, you need some smart logic to make it work. Here’s is a method that I use for this purpose. It’s simple to use, but a little too complex for such a simple task, which I think should have been a part of the DateTime class to begin with.
public static int NumberOfWeeks(DateTime dateFrom, DateTime dateTo)
   TimeSpan Span = dateTo.Subtract(dateFrom);

   if (Span.Days <= 7)
      if (dateFrom.DayOfWeek > dateTo.DayOfWeek)
         return 2;

      return 1;

   int Days = Span.Days - 7 + (int)dateFrom.DayOfWeek;
   int WeekCount = 1;
   int DayCount = 0;

   for (WeekCount = 1; DayCount < Days; WeekCount++)
      DayCount += 7;

   return WeekCount;

As you can see from these code examples, working with weeks in C# is not that obvious.