protected void Page_Init(object sender, EventArgs e)
    {
        ctx = new AriClinicContext("AriClinicContext");
        // security control, it must be a user logged
        if (Session["User"] == null)
            Response.Redirect("Default.aspx");
        else
        {
            user = (User)Session["User"];
            user = CntAriCli.GetUser(user.UserId, ctx);
            Process proc = (from p in ctx.Processes
                            where p.Code == "labtestassigned"
                            select p).FirstOrDefault<Process>();
            per = CntAriCli.GetPermission(user.UserGroup, proc, ctx);
            btnAccept.Visible = per.Modify;
        }

        // 
        if (Request.QueryString["LabTestAssignedId"] != null)
        {
            labTestcAssignedId = Int32.Parse(Request.QueryString["LabTestAssignedId"]);
            labTestAssigned = CntAriCli.GetLabTestAssigned(labTestcAssignedId, ctx);
            LoadData(labTestAssigned);
        }
        else
        {
            rdpLabTestDate.SelectedDate = DateTime.Now;
        }
        //
        if (Request.QueryString["PatientId"] != null)
        {
            patientId = int.Parse(Request.QueryString["PatientId"]);
            patient = CntAriCli.GetPatient(patientId, ctx);
            // fix rdc with patient
            rdcPatient.Items.Clear();
            rdcPatient.Items.Add(new RadComboBoxItem(patient.FullName,patient.PersonId.ToString()));
            rdcPatient.SelectedValue = patient.PersonId.ToString();
            rdcPatient.Enabled = false;
        }
        //
        if (Request.QueryString["VisitId"] != null)
        {
            visitId = int.Parse(Request.QueryString["VisitId"]);
            visit = CntAriCli.GetVisit(visitId, ctx);
            patientId = visit.Patient.PersonId;
            patient = CntAriCli.GetPatient(patientId, ctx);
            // fix rdc with patient
            rdcPatient.Items.Clear();
            rdcPatient.Items.Add(new RadComboBoxItem(patient.FullName, patient.PersonId.ToString()));
            rdcPatient.SelectedValue = patient.PersonId.ToString();
            rdcPatient.Enabled = false;
        }
    }
 protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
 {
     RefreshGrid(true);
     if (e.Argument == "new")
     {
         //RadGrid1.CurrentPageIndex = RadGrid1.PageCount - 1;
         RadGrid1.Rebind();
     }
     if (e.Argument == "yes")
     {
         if (Session["DeleteId"] != null)
         {
             try
             {
                 labTestAssignedId = (int)Session["DeleteId"];
                 labTestAssigned = (from da in ctx.LabTestAssigneds
                                       where da.LabTestAssignedId == labTestAssignedId
                                       select da).FirstOrDefault<LabTestAssigned>();
                 ctx.Delete(labTestAssigned);
                 ctx.SaveChanges();
                 RefreshGrid(true);
                 Session["DeleteId"] = null;
             }
             catch (Exception ex)
             {
                 Session["Exception"] = ex;
                 string command = String.Format("showDialog('Error','{0}','error',null, 0, 0)"
                                                , Resources.GeneralResource.DeleteRecordFail);
                 RadAjaxManager1.ResponseScripts.Add(command);
             }
         }
     }
 }
    protected void LoadData(LabTestAssigned lta)
    {
        // Load patient data
        rdcPatient.Items.Clear();
        rdcPatient.Items.Add(new RadComboBoxItem(lta.Patient.FullName, lta.Patient.PersonId.ToString()));
        rdcPatient.SelectedValue = lta.Patient.PersonId.ToString();

        // Load diagnostic data
        rdcLabTest.Items.Clear();
        rdcLabTest.Items.Add(new RadComboBoxItem(lta.LabTest.Name, lta.LabTest.LabTestId.ToString()));
        rdcLabTest.SelectedValue = lta.LabTest.LabTestId.ToString();

        rdpLabTestDate.SelectedDate = lta.LabTestDate;
        if (lta.LabTest.GeneralType == "LBTN")
        {
            //txtValue.Text = lta.NumValue.ToString();
            txtValue.Text = String.Format("{0:#.#}", lta.NumValue);
        }
        else 
        {
            txtValue.Text = lta.StringValue;
        }
        txtComments.Text = lta.Comments;
    }
 protected void UnloadData(LabTestAssigned lta)
 {
     lta.Patient = CntAriCli.GetPatient(int.Parse(rdcPatient.SelectedValue), ctx);
     lta.LabTestDate = (DateTime)rdpLabTestDate.SelectedDate;
     lta.LabTest = CntAriCli.GetLabTest(int.Parse(rdcLabTest.SelectedValue), ctx);
     if (lta.LabTest.GeneralType == "LBTN")
     {
         lta.NumValue = decimal.Parse(txtValue.Text);
     }
     else
     {
         lta.StringValue = txtValue.Text;
     }
     if (visit != null)
         lta.BaseVisit = visit; 
     lta.Comments = txtComments.Text;
 }
 protected bool CreateChange()
 {
     if (!DataOk())
         return false;
     if (labTestAssigned == null)
     {
         labTestAssigned = new LabTestAssigned();
         UnloadData(labTestAssigned);
         ctx.Add(labTestAssigned);
     }
     else
     {
         labTest = CntAriCli.GetLabTest(labTestId, ctx);
         UnloadData(labTestAssigned);
     }
     ctx.SaveChanges();
     return true;
 }