Click here to Skip to main content
Licence CPOL
First Posted 8 Mar 2009
Views 51,482
Downloads 2,219
Bookmarked 79 times

Google like Calendar in ASP.NET (AJAX)

By | 8 Mar 2009 | Article
This article describes how to manage events in calender.
 
Part of The SQL Zone sponsored by
See Also

Introduction

If you have got a Gmail account, you can access and use the Google calendar where you can add, update and delete events. You can also drag and drop an event from one date to another. There is also a facility to resize the event.

Now I tried to cover most functionalities with the help of AJAX. When you add an event in Google calendar, it shows a window just like tool tip where you can add or delete an event and here I opened a new window to add an event.

You have also got a facility to set the background color of an event in my code.

How It Works?

  • New event: Hold your mouse down and drag down
  • Move event: Hold your mouse down at the top of an event and drag
  • Resize event: Hold your mouse down at the bottom of an event and drag
  • Edit event: Double click at the middle of an event and wait until a new popup appears
  • Delete event: Click on event and press delete key on your keyboard or click on close button top left corner

Using the Code

You just have to create a database on SQL Server and run the SQL query attached with the article in zip format. Change the connection string according to your convenience in the application's web.config file.

HTML Code

The default time settings in calendar are in HH:MM AM/PM format. If you want to set the time in 24 hour format, please read the following code. Make the following changes in Calender.aspx page.

    <div id="demo_cal_hours">
        <%
            string suffix="";
            string hour = "";            
           
            for (int no = startHourOfWeekPlanner; no <= endHourOfWeekPlanner; no++)
            { 
                              
                hour = no.ToString();
               // un-comment if you want to set the time in 24 hours format
               // suffix = "0";               
               // comment the following 2 lines of code if you want to 
	      // set the timing in 24 hours
                 suffix = origin.ToString("tt");
                 hour = origin.ToString("hh");   
                time = "<span style='font-size:16px;'>" + hour.ToString() + 
		"</span>" + "<span class=\"content_hour\" >" + suffix + "</span>";
                origin = origin.AddMinutes(60);
                
          %>
          <div class="demoContentTime"><% Response.Write(time); %></div>
          
  <%
            } %>
  
  </div>

C# Code

Make the changes in the following code if you want to set the start and end Hour in the calendar. Make changes in startHourOfWeekPlanner and endHourOfWeekPlanner global variables. You can get this code in Calendar.aspx page.

public int startHourOfWeekPlanner = 8; // Start hour of Calender
public int endHourOfWeekPlanner = 21;  // End hour of Calender.
public double date = 0;
public string time="";
public DateTime origin;
// The following code converts DateTime to Unix timestamp.
protected void Page_Load(object sender, EventArgs e)
{    
    string dat = "1970-1-1";
    origin = new DateTime(DateTime.Now.Year, 
    DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0, 0);
    TimeSpan diff = origin - Convert.ToDateTime(dat);
    date = Math.Floor(diff.TotalSeconds);
}

JavaScript Code

The following code is called on body load event. You can find the code in new-Cal.js file.

function initCalendar()
{
     demo_container = document.getElementById('demo_container');
     if(!document.all)demo_container.onclick = ffEndEdit;
     demo_cal_appointments = document.getElementById('demo_cal_appointments');
     var subDivs = demo_cal_appointments.getElementsByTagName('DIV');
     for(var no=0;no<subDivs.length;no++){
      if(subDivs[no].className=='demo_appointHour'){
       subDivs[no].onmousedown = initNewAppointment;
   
       if(!SetnewAppointmentWidth)SetnewAppointmentWidth = subDivs[no].offsetWidth;
          }
      if(subDivs[no].className=='demo_appoint_day'){
       dayPositionArr[dayPositionArr.length] = getLeftPos(subDivs[no]);
      }
  
     }
     if(initilizeTopHours > CalenderStartHour)document.getElementById
	('demo_cal_content').scrollTop = ((initilizeTopHours - CalenderStartHour)*
	(itemRowHeight+1));
 
     SetappointmentsOffsetTop = getTopPos(demo_cal_appointments);
     SetappointmentsOffsetLeft = 2 - Number(appointMarginSize);
 
     document.documentElement.onmousemove = schedulerMouseMove;
     document.documentElement.onselectstart = cancelSelectionEvent;
     document.documentElement.onmouseup = schedulerMouseUp;
     document.documentElement.onkeydown = schedulerKeyboardEvent;
     var tmpDate = new Date();
     var dateItems = initDateToShow.split(/\-/g);
     tmpDate.setFullYear(dateItems[0]);
     tmpDate.setDate(Number(dateItems[2])/1);
     tmpDate.setMonth(Number(dateItems[1])/1-1);
     tmpDate.setHours(1);
     tmpDate.setMinutes(0);
     tmpDate.setSeconds(0);
 
      var day = tmpDate.getDay();
      if(day==0)day=7;
     if(day>1){
       var time = tmpDate.getTime();
      time = Number(time) - (1000*60*60*24) * (Number(day)-1);
      tmpDate.setTime(time); 
     }
     SetdateStartOfWeek = new Date(tmpDate);
 
     updateTopHeadDates();
 
     if(get_items_from_db){
      getItemsFromDBServer();  
    }
}

In the above script, function schedulerMouseMove is called on mousemove event, cancelSelectionEvent is called on selectstart event, schedulerMouseUp is called on mouseup event and schedulerKeyboardEvent is called on keydown event.

Compatible Browsers

I have tested the application on the following browsers: Internet Explorer 7, Firefox 2.0.0.20.

History

  • First version uploaded on March-08-2009

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Lokesh_1937444

CEO
SL Soft Technologies
India India

Member

SL-Soft Technologies provides e-business solutions and application management services to its various clients in India and abroad. Our expertise extends across a wide range of technologies encompassing Internet, E-Commerce, Data Warehousing, and Software Development. Our industry experience extends to providing solutions to several industries.
 
We believe in partnering with our clients for every solution we develop for them. Be it a Website Development, software development or any other assignment, we are always next to our clients. This gives us a unique opportunity to understand their business requirements and deliver them the solutions which fit the best in their needs. SL-Soft Technologies is on a continuous growth path ever since and has earned a clientele of highly satisfied customers. We are committed to set new horizons in client satisfaction.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGoogle Like calender with Only Day view PinmemberJai.chow5:44 28 Jan '12  
Bugit overlaps when two appointment at same time [modified] Pinmemberdharam123:20 11 Jan '12  
GeneralAwesome work PinmemberMember 83350681:48 3 Nov '11  
QuestionGoogle Like Calendar in Asp.Net(Ajax) Pinmemberbharanisoft2:43 20 Jul '11  
GeneralHELP - How to close the popup event edit window when the mouse focus moves back to the parent calendar window Pinmemberying xiao12:28 14 Jun '11  
GeneralI have few issues PinmemberVandana8722:05 10 Apr '11  
GeneralRe: I have few issues PinmemberLokesh_193744423:14 1 Jun '11  
GeneralGood PinmemberKatareRaju18:14 1 Mar '11  
GeneralNICE PinmemberKatareRaju18:13 1 Mar '11  
GeneralI need help plsss Pinmemberyuyu1917:39 12 Jan '11  
GeneralGOOD Pinmemberangxuan22:28 9 Jan '11  
GeneralMy vote of 4 Pinmemberabhi_jain16072:16 1 Oct '10  
GeneralGreat Work! Pinmembersrkrishnakumar15:27 8 Sep '10  
GeneralDear Lokesh Great Job Done PinmemberMilton_win10:23 12 Apr '10  
QuestionI want to modified the calendar into single line, but I got a problem. Pinmemberamory62622:11 23 Jan '10  
Questionlooks great but does not load the saved dates form db... PinmemberMember 44687668:33 17 Dec '09  
AnswerRe: looks great but does not load the saved dates form db... PinmemberLokesh_193744418:29 29 Dec '09  
QuestionHow Event Fire when Schedule come ?? Pinmemberketan d patel17:24 16 Dec '09  
AnswerRe: How Event Fire when Schedule come ?? PinmemberLokesh_193744418:27 29 Dec '09  
Questioncentered google calendar working wrong Pinmemberdolhaig0:17 6 Dec '09  
AnswerRe: centered google calendar working wrong PinmemberLokesh_19374442:45 9 Dec '09  
GeneralRe: centered google calendar working wrong Pinmemberdolhaig9:12 9 Dec '09  
GeneralMsSql loading problem Pinmemberdolhaig2:14 5 Dec '09  
Generalproblem in running Pinmemberindiaindia20:02 21 Oct '09  
GeneralRe: problem in running PinmemberLokesh_193744418:47 22 Oct '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Mar 2009
Article Copyright 2009 by Lokesh_1937444
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid