protected void btnExport_Click(object sender, EventArgs e)
        {
            string fileName = "goal_" + DateTime.Now.ToString("yyyyMMdd") + ".csv";

            Response.Clear();
            Response.Buffer = true;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
            Response.ContentType = "text/csv";
            Response.Charset     = "utf-8";
            StreamWriter sw      = new StreamWriter(Response.OutputStream);
            MyDataModel  context = new MyDataModel();

            int pageIndex = GridView1.PageIndex;

            for (int i = 1; i < GridView1.Columns.Count - 1; i++)
            {
                sw.Write(GridView1.Columns[i] + ",");
            }
            sw.Write(sw.NewLine);

            for (int n = 0; n < GridView1.PageCount; n++)
            {
                GridView1.PageIndex = n;
                BindData();

                foreach (GridViewRow dr in GridView1.Rows)
                {
                    for (int i = 1; i < GridView1.Columns.Count - 1; i++)
                    {
                        string cellValue = dr.Cells[i].Text;
                        cellValue = cellValue.Replace("\r\n", "\\r\\n ");

                        if (i == 1)
                        {
                            Control ctrl = dr.Cells[1].FindControl("chkDone");
                            sw.Write(((CheckBox)ctrl).Text + ',');
                        }
                        else if (i == 2)
                        {
                            Control ctrl     = dr.FindControl("HiddenTaskID");
                            int     taskID   = int.Parse(((Label)ctrl).Text);
                            string  taskName = T_TASKservice.GetTaskByID(context, taskID).TaskName;
                            sw.Write(taskName + ',');
                        }
                        else
                        {
                            sw.Write(cellValue + ',');
                        }
                    }

                    sw.Write(sw.NewLine);
                }
            }
            sw.Close();
            Response.End();
            GridView1.PageIndex = pageIndex;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            MyDataModel context = new MyDataModel();
            int         goalID;

            if (!int.TryParse(Request.QueryString["GoalID"], out goalID))
            {
                lblGoalName.Text      = Messages.goalName + Messages.errorMissingGoalID;
                lblGoalName.ForeColor = Color.Red;
                return;
            }

            string taskName = tbxTaskName.Text;

            if (string.IsNullOrEmpty(taskName))
            {
                taskName = "noname";
            }
            string description = tbxDescription.Text;
            bool   isDone      = bool.Parse(dlstDone.SelectedItem.Value);

            T_GOAL goal = T_GOALservice.GetGoalByID(context, goalID);

            if (goal == null)
            {
                lblGoalName.Text      = Messages.goalName + Messages.errorIncorrectGoalID;
                lblGoalName.ForeColor = Color.Red;
                return;
            }

            int taskID;

            //create(if TaskID absent) or update task
            if (int.TryParse(Request.QueryString["TaskID"], out taskID))
            {
                if (!T_TASKservice.IsTaskExists(context, taskID))
                {
                    return;
                }
                T_TASK task = T_TASKservice.GetTaskByID(context, taskID);
                task.GoalID      = goalID;
                task.TaskName    = taskName;
                task.Description = description;
                task.Done        = isDone;
                task.UpdateDate  = DateTime.Now;
            }
            else
            {
                T_TASK newTask = new T_TASK {
                    GoalID = goalID, TaskName = taskName, Description = description, Done = isDone, UpdateDate = DateTime.Now, T_GOAL = goal
                };
                context.T_TASK.Add(newTask);
            }
            context.SaveChanges();
            Response.Redirect("~/_GoalPage.aspx?GoalID=" + goalID);
        }
        protected void chkDone_CheckedChanged(object sender, EventArgs e)
        {
            MyDataModel context  = new MyDataModel();
            CheckBox    checkbox = (CheckBox)sender;
            GridViewRow row      = (GridViewRow)((checkbox).NamingContainer);

            int taskID = int.Parse(((Label)row.FindControl("HiddenTaskID")).Text.Trim());

            if (checkbox.Checked)
            {
                T_TASKservice.GetTaskByID(context, taskID).Done = true;
            }
            else
            {
                T_TASKservice.GetTaskByID(context, taskID).Done = false;
            }
            context.SaveChanges();
            BindData();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            MyDataModel context              = new MyDataModel();
            int         tasknameMaxLength    = 50;
            int         descriptionMaxLength = 50;

            Label1.Text = tbxDescription.Text.Length.ToString();
            tbxDescription.Attributes.Add("MaxLength", tbxDescription.MaxLength.ToString());
            string actualUserName = Page.User.Identity.Name;

            int goalID;

            if (!int.TryParse(Request.QueryString["GoalID"], out goalID))
            {
                tbxTaskName.MaxLength  = tasknameMaxLength;
                tbxTaskName.ToolTip    = string.Format("Max {0} characters", tasknameMaxLength);
                tbxDescription.ToolTip = string.Format("Max {0} characters", descriptionMaxLength);
                lblGoalName.Text       = Messages.goalName + Messages.errorMissingGoalID;
                lblGoalName.ForeColor  = Color.Red;
                return;
            }
            else
            {
                T_GOAL goalAbsentOrDeleted = T_GOALservice.GetGoalByID(context, goalID);
                if (goalAbsentOrDeleted == null)
                {
                    BadGoalID(Messages.goalName + " is absent!");
                    return;
                }
                else if (goalAbsentOrDeleted.T_ACCOUNT.Email != actualUserName)
                {
                    BadGoalID(Messages.goalName + " is incorrect!");
                    return;
                }
                else
                {
                    lblGoalName.ForeColor = Color.Empty;
                    btnSave.Enabled       = true;
                    lblGoalName.Text      = Messages.goalName + goalAbsentOrDeleted.GoalName;
                }
            }

            if (!IsPostBack)
            {
                int taskID;

                if (int.TryParse(Request.QueryString["TaskID"], out taskID))
                {
                    if (!T_TASKservice.IsTaskExists(context, taskID))
                    {
                        return;
                    }
                    T_TASK task = T_TASKservice.GetTaskByID(context, taskID);
                    tbxTaskName.Text       = task.TaskName;
                    tbxDescription.Text    = task.Description;
                    Label1.Text            = task.Description.Count().ToString();
                    dlstDone.SelectedValue = task.Done.ToString().ToLower();
                }
                else
                {
                    Label1.Text = tbxDescription.Text.Length.ToString();
                }
            }
        }