protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (tempTaskId!=-1)
            {
                using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
                {

                        var calendar = (from c in db.Calendars
                                        where c.TaskId == tempTaskId
                                        select c).FirstOrDefault();
                        if (calendar !=null)
                        {
                            tempDateString = calendar.date;
                            // split date
                            string[] dates = tempDateString.Split(' ');
                            // draw date(s) that has been selected 
                            for (int i = 0; i < dates.Length; i++)
                            {
                                if (e.Day.Date.ToString("yyyy-MM-dd") == dates[i])
                                {
                                    e.Cell.BackColor = System.Drawing.Color.Red;
                                }
                            }
                        }
                        else
                        {
                        label1.Visible = true;
                        label1.Text = "There are no record.";
                    }
            }
               
            }                           
        }
        //save buton event
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
            {

                Models.Task t = new Models.Task();
                int TaskID = 0;
                if (Request.QueryString["TaskId"] != null)
                {
                    TaskID = Convert.ToInt32(Request.QueryString["TaskId"]);
                    t = (from task in db.Tasks
                         where task.TaskId == TaskID
                         select task).FirstOrDefault();
                }
                t.TaskName = TxtTaskName.Text;
                t.TaskDueDay = Convert.ToDateTime(TxtDueDay.Text);
                t.UserId = User.Identity.GetUserId();
                // if there is no taskID on the query string, add this to db as a new object
                if (TaskID == 0)
                {
                    db.Tasks.Add(t);
                }

                db.SaveChanges();
                Response.Redirect("Tasks.aspx");
            }
        }
 protected void getTasks()
 {
     String tempUserId = User.Identity.GetUserId();
     using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
     {
         var t = from task in db.Tasks
                 where task.UserId == tempUserId
                 select task;
         GridView1.DataSource = t.ToList();
         GridView1.DataBind();
     }
 }
 protected void bindData()
 {
     String UserID = User.Identity.GetUserId();
     using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
     {
         var result = from t in db.Tasks
                      where t.UserId == UserID
                      select t;
         DropDownList1.DataSource = result.ToList();
         DropDownList1.DataBind();
         // add a Select to item list
         ListItem newItem = new ListItem("-Select-", "0");
         DropDownList1.Items.Insert(0, newItem);
     }
 }
        protected void getTask()
        {
            int TaskID = Convert.ToInt32(Request.QueryString["TaskId"]);
            using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
            {
                 TimeManagerIII.Models.Task
                t = (from task in db.Tasks
                     where task.TaskId == TaskID
                     select task).FirstOrDefault();
                // if there is task object based on taskID, fill the text box.
                 if (t!=null)
                 {
                     TxtDueDay.Text = t.TaskDueDay.ToString("yyyy-MM-dd");
                     TxtTaskName.Text = t.TaskName;
                 }
            }

        }
        protected void btn_Puch_Click(object sender, EventArgs e)
        {
            //Get the button that raised the event
            Button btn = (Button)sender;

            //Get the row that contains this button
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;

            //Get rowindex
            int rowindex = gvr.RowIndex;      

            // get the taskId of the row that clicked 

                int taskID = Convert.ToInt32(GridView1.DataKeys[rowindex].Values["TaskId"]);

                using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
                {
               
               var c = (from cdate in db.Calendars
                         where cdate.TaskId == taskID
                         select cdate).FirstOrDefault();
              // if user have punch card info, add " " before date, if not, add date
                if (c != null)
                {
                    c.TaskId = taskID;
                    c.date += " " + DateTime.Now.ToString("yyyy-MM-dd");
                   
                }
                else
                {
                    TimeManagerIII.Models.Calendar c2 = new TimeManagerIII.Models.Calendar();
                    c2.TaskId = taskID;
                    c2.date += DateTime.Now.ToString("yyyy-MM-dd");
                    db.Calendars.Add(c2);
                }               
                   db.SaveChanges();         
                }

            }
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
   
     int TaskID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["TaskId"]);
     using (chaodyzDBEntities1 db = new chaodyzDBEntities1())
     {
         var s = (from task in db.Tasks
                  where task.TaskId == TaskID
                  select task).FirstOrDefault();
         var c = from cdate in db.Calendars
                  where cdate.TaskId == TaskID
                   select cdate;
         //if there is calendar info for this user, remove them first
         if (c != null) {
             db.Calendars.RemoveRange(c);
     }
         db.Tasks.Remove(s);
       
         db.SaveChanges();
     }
     getTasks();
 }