protected void grdChildInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // btnAdd.Text = "Add Child";
        int p = Convert.ToInt32(e.RowIndex);

        dtTemp = (DataTable)ViewState["DT"];
        //GetAdmissionNo();
        if (dtTemp.Rows[p][0].ToString() != "0")
        {
            IInfantEnrollment ptnMgrPMTCT = (IInfantEnrollment)ObjectFactory.CreateInstance(ObjFactoryParameter);
            ptnMgrPMTCT.DeleteExposedInfantById(Convert.ToInt16(dtTemp.Rows[p][0]));
        }
        dtTemp.Rows[p].Delete();
        dtTemp.AcceptChanges();
        ViewState["DT"] = dtTemp;
        BindGrid((DataTable)ViewState["DT"]);
        IQCareMsgBox.Show("DeleteSuccess", this);
        if (((DataTable)ViewState["DT"]).Rows.Count == 0)
        {
            btnsave.Enabled         = false;
            grdChildInfo.DataSource = ViewState["DT"];
            grdChildInfo.DataBind();
        }
        else
        {
            btnsave.Enabled = true;
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        dtTemp = (DataTable)ViewState["DT"];
        for (int i = 0; i < dtTemp.Rows.Count; i++)
        {
            Hashtable         theHT       = AddUpdateData(i);
            IInfantEnrollment ptnMgrPMTCT = (IInfantEnrollment)ObjectFactory.CreateInstance(ObjFactoryParameter);
            //DataTable theCustomDataDT = new DataTable();
            /*DataTable*/
            DateTime?ddeath = null;
            if (!theHT["deathdate"].Equals(string.Empty))
            {
                ddeath = Convert.ToDateTime(theHT["deathdate"]);
            }
            int ctx2Id = 0;
            if (theHT["ctx"].ToString() == "Yes")
            {
                ctx2Id = 1;
            }
            int theDS = ptnMgrPMTCT.SaveExposedInfant(Convert.ToInt32(theHT["id"]), Convert.ToInt32(theHT["ptn_pk"]),
                                                      Convert.ToInt32(theHT["ExposedInfantId"]),
                                                      theHT["fname"].ToString(), theHT["lname"].ToString(), Convert.ToDateTime(theHT["dob"]),
                                                      theHT["fp"].ToString(), ctx2Id.ToString(), theHT["type"].ToString(),
                                                      theHT["result"].ToString(), theHT["final"].ToString(), ddeath, Convert.ToInt32(Session["AppUserId"].ToString()));

            if (dtTemp.Rows[i][0].ToString() == "0")
            {
                ViewState["Status"] = "Add";
                //ViewState["visitPk"] = theDS.Rows[0]["Visit_ID"].ToString();
                //ViewState["PtnID"] = theDS.Rows[0]["PatientID"].ToString();
                //DataSet theDSInfantInfo = ptnMgrPMTCT.SaveInfantInfo(Convert.ToInt64(ViewState["PtnID"]), Convert.ToInt64(Session["AppLocationId"]), Convert.ToInt64(ViewState["visitPk"]), Convert.ToInt64(Session["PatientId"]), Convert.ToInt64(Session["AppUserId"]));
                BindGrid(dtTemp);
            }
            else
            {
                ViewState["Status"] = "Edit";
                //Hashtable theHT = AddUpdateData(i);
                //IPatientRegistration ptnMgrPMTCT = (IPatientRegistration)ObjectFactory.CreateInstance(ObjFactoryParameter);
                //DataTable theCustomDataDT = new DataTable();
                //DataTable theDS = ptnMgrPMTCT.SavePatientRegistrationPMTCT(theHT, theCustomDataDT);
                BindGrid(dtTemp);
            }
            SaveCancel();
        }
    }
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        if (ViewState["DT"] != null)
        {
            dtTemp = (DataTable)ViewState["DT"];
        }
        if (btnAdd.Text == "Add Infant")
        {
            IInfantEnrollment ptnMgrPMTCT = (IInfantEnrollment)ObjectFactory.CreateInstance(ObjFactoryParameter);
            DataSet           theDS       = ptnMgrPMTCT.CheckIdentity(TxtInfantId.Text);
            if (theDS.Tables[0].Rows.Count > 0)
            {
                IQCareMsgBox.Show("InfantDataDBExist", this);
                TxtInfantId.Focus();
                return;
            }
            if (grdChildInfo.Rows.Count > 0)
            {
                for (int i = 0; i < grdChildInfo.Rows.Count; i++)
                {
                    if (grdChildInfo.Rows[i].Cells[2].Text == TxtInfantId.Text)
                    {
                        IQCareMsgBox.Show("InfantDataDBExist", this);
                        TxtInfantId.Focus();
                        return;
                    }
                }
            }
        }
        else
        {
            if (dtTemp.Rows.Count > 0)
            {
                int    r     = Convert.ToInt32(Session["SelectedRow"]);
                string InfID = dtTemp.Rows[r][2].ToString();
                if (InfID != TxtInfantId.Text)
                {
                    IInfantEnrollment ptnMgrPMTCT = (IInfantEnrollment)ObjectFactory.CreateInstance(ObjFactoryParameter);
                    DataSet           theDS       = ptnMgrPMTCT.CheckIdentity(TxtInfantId.Text);
                    if (theDS.Tables[0].Rows.Count > 0)
                    {
                        IQCareMsgBox.Show("InfantDataDBExist", this);
                        TxtInfantId.Focus();
                        return;
                    }
                }

                if (grdChildInfo.Rows.Count > 0)
                {
                    for (int i = 0; i < grdChildInfo.Rows.Count; i++)
                    {
                        if (grdChildInfo.Rows[i].Cells[2].Text == TxtInfantId.Text && i != r)
                        {
                            IQCareMsgBox.Show("InfantDataDBExist", this);
                            TxtInfantId.Focus();
                            return;
                        }
                    }
                }
            }
        }
        if (Authentiaction.HasFunctionRight(ApplicationAccess.ChildEnrollment, FunctionAccess.Add, (DataTable)Session["UserRight"]) == false)
        {
            btnsave.Enabled = false;
            btnAdd.Enabled  = false;
        }
        else
        {
            btnsave.Enabled = true;
            btnAdd.Enabled  = true;
        }

        if (FieldValidation() == false)
        {
            return;
        }

        if (btnAdd.Text == "Add Infant")
        {
            icount = dtTemp.Rows.Count;
            DataRow row;
            row                    = dtTemp.NewRow();
            row["Id"]              = "0";
            row["FirstName"]       = ViewState["FirstName"].ToString();
            row["LastName"]        = ViewState["LastName"].ToString();
            row["ExposedInfantId"] = TxtInfantId.Text;
            string   strDob = TxtDateOfBirth.Value;
            DateTime dtDob  = Convert.ToDateTime(strDob);
            strDob     = dtDob.ToString("dd MMM yyyy");
            row["DOB"] = strDob;
            if (DDInfantFeedingPractice.SelectedItem.Text == "Select")
            {
                row["FeedingPractice3mos"] = string.Empty;
            }
            else
            {
                row["FeedingPractice3mos"] = string.IsNullOrEmpty(DDInfantFeedingPractice.SelectedValue) ?
                                             string.Empty : DDInfantFeedingPractice.SelectedItem.Text;
            }
            row["CTX2mos"] = ChkCPTStarted.Checked ? "Yes" : "No";

            if (DDHIVTestType.SelectedItem.Text == "Select")
            {
                row["HIVTestType"] = string.Empty;
            }
            else
            {
                row["HIVTestType"] = string.IsNullOrEmpty(DDHIVTestType.SelectedValue) ?
                                     string.Empty : DDHIVTestType.SelectedItem.Text;
            }
            if (DDResult.SelectedItem.Text == "Select")
            {
                row["HIVResult"] = string.Empty;
            }
            else
            {
                row["HIVResult"] = string.IsNullOrEmpty(DDResult.SelectedValue) ?
                                   string.Empty : DDResult.SelectedItem.Text;
            }
            if (DDFinalResult.SelectedItem.Text == "Select")
            {
                row["FinalStatus"] = string.Empty;
            }
            else
            {
                row["FinalStatus"] = string.IsNullOrEmpty(DDFinalResult.SelectedValue) ?
                                     string.Empty : DDFinalResult.SelectedItem.Text;
            }
            if (!TxtDeathDate.Text.Equals(string.Empty))
            {
                string   strDeath = TxtDeathDate.Text;
                DateTime dtDeath  = Convert.ToDateTime(strDeath);
                strDeath         = dtDeath.ToString("dd MMM yyyy");
                row["DeathDate"] = strDeath;
            }
            if (TxtDeathDate.Text == "")
            {
            }
            // New
            row["HivResultId"] = string.IsNullOrEmpty(DDResult.SelectedItem.Value) ?
                                 string.Empty : DDResult.SelectedItem.Value;
            row["FeedID"] = string.IsNullOrEmpty(DDInfantFeedingPractice.SelectedItem.Value) ?
                            string.Empty : DDInfantFeedingPractice.SelectedItem.Value;
            row["FinalStatusID"] = string.IsNullOrEmpty(DDFinalResult.SelectedItem.Value) ?
                                   string.Empty : DDFinalResult.SelectedItem.Value;
            row["HIVTestTypeID"] = string.IsNullOrEmpty(DDHIVTestType.SelectedItem.Value) ?
                                   string.Empty : DDHIVTestType.SelectedItem.Value;

            dtTemp.Rows.InsertAt(row, icount);
            ViewState["DT"]    = dtTemp;
            ViewState["Count"] = dtTemp.Rows.Count + 1;
            BindGrid(dtTemp);
            ClearControl();
        }
        else if (btnAdd.Text == "Update Infant")
        {
            int r = Convert.ToInt32(Session["SelectedRow"]);
            dtTemp.Rows[r]["FirstName"]       = TxtFirstName.Text;
            dtTemp.Rows[r]["LastName"]        = TxtLastName.Text;
            dtTemp.Rows[r]["ExposedInfantId"] = TxtInfantId.Text;
            string   strDob = TxtDateOfBirth.Value;
            DateTime dtDob  = Convert.ToDateTime(strDob);
            strDob = dtDob.ToString("dd MMM yyyy");
            dtTemp.Rows[r]["DOB"] = strDob;

            if (DDInfantFeedingPractice.SelectedItem.Text == "Select")
            {
                dtTemp.Rows[r]["FeedingPractice3mos"] = string.Empty;
            }
            else
            {
                dtTemp.Rows[r]["FeedingPractice3mos"] = string.IsNullOrEmpty(DDInfantFeedingPractice.SelectedValue) ?
                                                        string.Empty : DDInfantFeedingPractice.SelectedItem.Text;
            }


            dtTemp.Rows[r]["CTX2mos"] = ChkCPTStarted.Checked ? "Yes" : "No";

            if (DDHIVTestType.SelectedItem.Text == "Select")
            {
                dtTemp.Rows[r]["HIVTestType"] = string.Empty;
            }
            else
            {
                dtTemp.Rows[r]["HIVTestType"] = string.IsNullOrEmpty(DDHIVTestType.SelectedValue) ?
                                                string.Empty : DDHIVTestType.SelectedItem.Text;
            }

            if (DDResult.SelectedItem.Text == "Select")
            {
                dtTemp.Rows[r]["HIVResult"] = string.Empty;
            }
            else
            {
                dtTemp.Rows[r]["HIVResult"] = string.IsNullOrEmpty(DDResult.SelectedValue) ?
                                              string.Empty : DDResult.SelectedItem.Text;
            }
            if (DDFinalResult.SelectedItem.Text == "Select")
            {
                dtTemp.Rows[r]["FinalStatus"] = string.Empty;
            }
            else
            {
                dtTemp.Rows[r]["FinalStatus"] = string.IsNullOrEmpty(DDFinalResult.SelectedValue) ?
                                                string.Empty : DDFinalResult.SelectedItem.Text;
            }

            if (!TxtDeathDate.Text.Equals(string.Empty))
            {
                string   strDeath = TxtDeathDate.Text;
                DateTime dtDeath  = Convert.ToDateTime(strDeath);
                strDeath = dtDeath.ToString("dd MMM yyyy");
                dtTemp.Rows[r]["DeathDate"] = strDeath;
            }
            if (TxtDeathDate.Text == "")
            {
                dtTemp.Rows[r]["DeathDate"] = System.DBNull.Value;
            }
            dtTemp.Rows[r]["HivResultId"] = string.IsNullOrEmpty(DDResult.SelectedItem.Value) ?
                                            string.Empty : DDResult.SelectedItem.Value;
            dtTemp.Rows[r]["FeedID"] = string.IsNullOrEmpty(DDInfantFeedingPractice.SelectedItem.Value) ?
                                       string.Empty : DDInfantFeedingPractice.SelectedItem.Value;
            dtTemp.Rows[r]["FinalStatusID"] = string.IsNullOrEmpty(DDFinalResult.SelectedItem.Value) ?
                                              string.Empty : DDFinalResult.SelectedItem.Value;
            dtTemp.Rows[r]["HIVTestTypeID"] = string.IsNullOrEmpty(DDHIVTestType.SelectedItem.Value) ?
                                              string.Empty : DDHIVTestType.SelectedItem.Value;
            ViewState["DT"] = dtTemp;
            BindGrid(dtTemp);
            btnAdd.Text = "Add Infant";
            ClearControl();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AppLocation"] == null || Session.Count == 0 || Session["AppUserID"].ToString() == "")
        {
            IQCareMsgBox.Show("SessionExpired", this);
            Response.Redirect("~/frmlogin.aspx", true);
        }
        maintainviewState(Convert.ToString(DDFinalResult.SelectedItem));
        BusineesRuleFinalResult();

        if (!Page.IsPostBack)
        {
            AddAttributes();
            IInfantEnrollment ptnMgrPMTCT = (IInfantEnrollment)ObjectFactory.CreateInstance(ObjFactoryParameter);
            theDS  = ptnMgrPMTCT.GetExposedInfantByParentId(Convert.ToInt16(Session["PatientId"].ToString()));
            dtTemp = theDS.Tables[0];
            //dtInfo = theDS.Tables[1];
            string   strPatientName = Session["PatientName"].ToString();
            string[] strname        = strPatientName.Split(',');
            ViewState["FName"] = strname[1].ToString();
            ViewState["LName"] = strname[0].ToString();
            (Master.FindControl("levelTwoNavigationUserControl1").FindControl("lblpntStatus") as Label).Text = Session["lblpntstatus"].ToString();
            if (Authentiaction.HasFunctionRight(ApplicationAccess.ChildEnrollment, FunctionAccess.View, (DataTable)Session["UserRight"]) == false)
            {
                string theUrl = string.Empty;
                theUrl = string.Format("../ClinicalForms/frmPatient_Home.aspx");
                Response.Redirect(theUrl);
            }
            else if (Authentiaction.HasFunctionRight(ApplicationAccess.ChildEnrollment, FunctionAccess.Add, (DataTable)Session["UserRight"]) == false)
            {
                btnsave.Enabled = false;
                btnAdd.Enabled  = false;
            }
            else if (Authentiaction.HasFunctionRight(ApplicationAccess.ChildEnrollment, FunctionAccess.Update, (DataTable)Session["UserRight"]) == false)
            {
                btnsave.Enabled = false;
                btnAdd.Enabled  = false;
            }

            // Bind Grid

            //GetAdmissionNo();


            (Master.FindControl("levelTwoNavigationUserControl1").FindControl("lblpntStatus") as Label).Text = Session["lblpntstatus"].ToString();
            //(Master.FindControl("lblRoot") as Label).Text = "";
            //(Master.FindControl("lblMark") as Label).Visible = false;
            //(Master.FindControl("lblheader") as Label).Text = "Exposed Infant Follow up";
            //(Master.FindControl("lblformname") as Label).Text = "Exposed Infant Follow up";
            (Master.FindControl("levelOneNavigationUserControl1").FindControl("lblRoot") as Label).Visible  = false;
            (Master.FindControl("levelOneNavigationUserControl1").FindControl("lblheader") as Label).Text   = "Exposed Infant Follow up";
            (Master.FindControl("levelTwoNavigationUserControl1").FindControl("lblformname") as Label).Text = "Exposed Infant Follow up";
            //lblname.Text = strPatientName;
            //DataTable dtPatientInfo = (DataTable)Session["PatientInformation"];
            //if (dtPatientInfo != null)
            //{
            //    lblIQnumber.Text = dtPatientInfo.Rows[0]["IQNumber"].ToString();
            //}
            if (ViewState["DT"] == null)
            {
                ViewState["DT"] = dtTemp;
                //ViewState["iSerialNo"] = Convert.ToInt16(dtInfo.Rows[0][0]) + 1;
                ViewState["FirstName"] = "Baby of " + strname[1].ToString();
                ViewState["LastName"]  = strname[0].ToString();
                //TxtFirstName.Text = ViewState["FirstName"].ToString();
                //TxtLastName.Text = ViewState["LastName"].ToString();
            }
            else
            {
                ViewState["FirstName"] = TxtFirstName.Text;
                ViewState["LastName"]  = TxtLastName.Text;
                //TxtFirstName.Text = ViewState["FirstName"].ToString();
                //TxtLastName.Text = ViewState["LastName"].ToString();
            }

            Init_Form();
            BindGrid(dtTemp);
            if (dtTemp.Rows.Count == 0)
            {
                btnsave.Enabled = false;
            }
            else
            {
                btnsave.Enabled = true;
            }
        }
    }