private void PhonesChangesUpdate(cUser Demography) { List <cPhone> phones = Session["dem_Phones"] as List <cPhone>; int userId = (int)Session["UserID"]; //For each element of the original list perform one of the following: //If update, update information //If delete, mark record for delete //If new, mark record for adding cPhone a1 = null; if (Demography.UserPhones != null) { foreach (cPhone a in Demography.UserPhones) //state in database { a1 = phones.FirstOrDefault(x => x.PhoneNumberID == a.PhoneNumberID); //If record not found in memory that means it was deleted if (a1 == null) { a.SaveUpdate(userId, true); } else { a1.SaveUpdate(userId); } } } a1 = phones.FirstOrDefault(x => x.PhoneNumberID <= 0); if (a1 != null && a1.IsValid()) // If new and valid, let push it to the database { a1.SaveUpdate(userId); } }
protected void gv_PhoneNums_RowUpdating(object sender, GridViewUpdateEventArgs e) { List <cPhone> phones = null; int gindex = e.RowIndex; cPhone phone = new cPhone();; GridView gv = (GridView)sender; phones = Session["dem_Phones"] as List <Classes.cPhone>; if (gindex < phones.Count()) { phone.AreaCode = ((gv.Rows[gindex].FindControl("gv_txtAreaCode") as TextBox).Text + string.Empty).Trim(); phone.PhoneNumber = ((gv.Rows[gindex].FindControl("gv_txtPhoneNumber") as TextBox).Text + string.Empty).Trim(); phone.Extension = ((gv.Rows[gindex].FindControl("gv_txtExtension") as TextBox).Text + string.Empty).Trim(); int iRetVal = 0; int.TryParse((gv.Rows[gindex].FindControl("ddPhoneNumber") as DropDownList).SelectedValue, out iRetVal); //only native types can be returned so temp variable phone.PhoneTypeID = iRetVal; phone.IsPrimary = (gv.Rows[gindex].FindControl("rbtnPrimary1") as RadioButton).Checked; if (phone.IsValid()) { phones[gindex].AreaCode = phone.AreaCode; phones[gindex].PhoneNumber = phone.PhoneNumber; phones[gindex].Extension = phone.Extension; phones[gindex].PhoneTypeID = phone.PhoneTypeID; phones[gindex].IsPrimary = phone.IsPrimary; if (phones[gindex].PhoneNumberID < 1) { phones.Add(new cPhone()); } //if the primary record is checked then all other record must not be ckeched if (phones[gindex].IsPrimary) { phones.ForAll(x => x.IsPrimary = false); phones[gindex].IsPrimary = true; } Session["dem_Phones"] = phones; gv.Rows[gindex].RowState = DataControlRowState.Normal; gv_PhoneNums.EditIndex = -1; } else { lblMessage.Text = phone.strErrorDescription; e.Cancel = true; } } BindAllGrids(); }
protected void gvPhoneNumbers_RowUpdating(object sender, GridViewUpdateEventArgs e) { List <cPhone> clPhoneNumbers = Session["dem_Phones"] as List <cPhone>; if (e.RowIndex < clPhoneNumbers.Count) { cPhone UpdatedPhone = clPhoneNumbers[e.RowIndex]; TextBox tbAreaCode = (TextBox)gvPhoneNumbers.Rows[e.RowIndex].FindControl("tbAreaCode"); if (tbAreaCode != null) { UpdatedPhone.AreaCode = tbAreaCode.Text; } TextBox tbPhoneNumber = (TextBox)gvPhoneNumbers.Rows[e.RowIndex].FindControl("tbPhoneNumber"); if (tbPhoneNumber != null) { UpdatedPhone.PhoneNumber = tbPhoneNumber.Text; } if (!UpdatedPhone.IsValid()) { lblMessage.Text = UpdatedPhone.strErrorDescription; e.Cancel = true; } else { TextBox tbExtension = (TextBox)gvPhoneNumbers.Rows[e.RowIndex].FindControl("tbExtension"); if (tbExtension != null) { UpdatedPhone.Extension = tbExtension.Text; } DropDownList ddlPhoneType = (DropDownList)gvPhoneNumbers.Rows[e.RowIndex].FindControl("ddlPhoneType"); if (ddlPhoneType != null) { UpdatedPhone.PhoneTypeID = ddlPhoneType.SelectedValue.ToInt32(); } DropDownList ddlProviderList = (DropDownList)gvPhoneNumbers.Rows[e.RowIndex].FindControl("ddlProviderList"); if (ddlProviderList != null) { UpdatedPhone.ProviderID = ddlProviderList.SelectedValue.ToInt32(); } RadioButton rbPrimary = (RadioButton)gvPhoneNumbers.Rows[e.RowIndex].FindControl("rbPrimary"); if (rbPrimary != null) { if (rbPrimary.Checked) { clPhoneNumbers.ForAll(x => x.IsPrimary = false); UpdatedPhone.IsPrimary = true; } } Session["dem_Phones"] = clPhoneNumbers; gvPhoneNumbers.EditIndex = -1; } BindPhoneNumbers(); } else { e.Cancel = true; } }