protected void DeleteContactButton_Click(object sender, EventArgs e) { bool isError = false; try { //Get the common web service instance. ServiceAccess serviceLoader = ServiceAccess.GetInstance(); FarmService.FarmService farmService = serviceLoader.GetFarm(); farmService.DeleteContact(Convert.ToInt64(ContactIdHiddenField.Value.ToString()), LoginUserId); } catch (Exception exception) { log.Error("UNKNOWN ERROR WHILE DELETE CONTACT:", exception); isError = true; if (exception.Message.Contains("This action will cause Farm to be deleted and the Parent-Farm is Active. Hence Contact Cannot be deleted")) { ErrorLiteral.Text = "Error: This action will cause Farm to be deleted and the Parent-Farm is Active. Hence Contact Cannot be deleted."; } else { ErrorLiteral.Text = "UNKNOWN ERROR WHILE DELETE CONTACT: Contact Administrator"; } } if (!isError) { int contactCount = int.Parse(ContactCountHiddenField.Value.ToString()); if (ParentPageHiddenField.Value.ToString() == "") { if (contactCount < 2) { if (DefaultPlotFlagHiddenField.Value.ToString().Equals("true")) { Response.Redirect("~/Members/FarmManagement.aspx"); } else { Response.Redirect("~/Members/ViewFarm.aspx?farmId=" + FarmIdHiddenField.Value.ToString()); } } else { Response.Redirect("~/Members/ViewPlot.aspx?plotId=" + PlotIdHiddenField.Value.ToString()); } } else if (ParentPageHiddenField.Value.ToString().Equals("SearchFarmData.aspx")) { Response.Redirect("~/Members/" + ParentPageHiddenField.Value.ToString() + "?accessType=old"); } } }
protected void Page_Load(object sender, EventArgs e) { // Register the client side script. ClientScriptManager csManager = Page.ClientScript; Type type = this.GetType(); string newLine = Environment.NewLine; if (!csManager.IsClientScriptBlockRegistered(type, "BodyLoadScript")) { StringBuilder csText = new StringBuilder(); csText.Append("<script type=\"text/javascript\">" + newLine); csText.Append("<!--" + newLine); csText.Append("function onBodyLoad() {" + newLine); csText.Append(" var allInputTags = document.getElementsByTagName(\"input\");" + newLine); csText.Append(" var msg = \"\";" + newLine); csText.Append(" var conformation;" + newLine); csText.Append(" for(i = 0; i < allInputTags.length; i++) {" + newLine); csText.Append(" if(allInputTags[i].type == \"hidden\") {" + newLine); csText.Append(" if(allInputTags[i].name.indexOf(\"DeleteMessageHiddenField\") >= 0)" + newLine); csText.Append(" msg = allInputTags[i].value;" + newLine); csText.Append(" else if(allInputTags[i].name.indexOf(\"DeleteConformationHiddenField\") >= 0)" + newLine); csText.Append(" conformation = allInputTags[i];" + newLine); csText.Append(" }" + newLine); csText.Append(" } " + newLine); csText.Append(" if (msg != \"\") {" + newLine); csText.Append(" if (confirm(msg))" + newLine); csText.Append(" conformation.value = \"true\";" + newLine); csText.Append(" else" + newLine); csText.Append(" conformation.value = \"false\";" + newLine); csText.Append(" document.forms[0].submit();" + newLine); csText.Append(" }" + newLine); csText.Append("}" + newLine); csText.Append("document.body.onload = onBodyLoad;" + newLine); csText.Append("// -->" + newLine); csText.Append("</script>"); csManager.RegisterClientScriptBlock(type, "BodyLoadScript", csText.ToString(), false); } if (!IsPostBack) { DeleteMessageHiddenField.Value = ""; DeleteConformationHiddenField.Value = ""; DeleteContactIdHiddenField.Value = ""; try { // Get the common web service instance. ServiceAccess serviceLoader = ServiceAccess.GetInstance(); FarmService.FarmService farmService = serviceLoader.GetFarm(); SearchCriteriaPane.Visible = true; SearchResultPane.Visible = false; if (IsAgentRole) { AgentListPanel.Visible = false; FarmDropDownList.DataSource = farmService.GetFarmListForUser(GetAgentId()); FarmDropDownList.DataValueField = "FarmId"; FarmDropDownList.DataTextField = "FarmName"; FarmDropDownList.DataBind(); FarmDropDownList.Items.Insert(0, new ListItem("All Farms", "0")); PlotDropDownList.Items.Add(new ListItem("All Plots", "0")); } else { AgentListPanel.Visible = true; SearchCriteriaPane.Visible = false; CommonService commonService = serviceLoader.GetCommon(); AgentListDropDownList.DataSource = commonService.GetAgentsList(); AgentListDropDownList.DataValueField = "UserId"; AgentListDropDownList.DataTextField = "UserName"; AgentListDropDownList.DataBind(); AgentListDropDownList.Items.Insert(0, "Select an Agent"); } ResultCountLiteral.Text = "Search for the Farm Contacts"; if ((Request.QueryString["accessType"] != "") && (Request.QueryString["accessType"] != null) && (Request.QueryString["accessType"] == "old")) { if (Session["SearchFarmDataWhere"] != null) { string where = Session["SearchFarmDataWhere"].ToString(); int pageIndex = 1; if (Session["SearchFarmDataPageIndex"] != null) { pageIndex = int.Parse(Session["SearchFarmDataPageIndex"].ToString()); } FarmService.FarmDetailsReportInfo[] farms = farmService.GetSearchFarmData(where); SearchFarmResultGridView.DataSource = farms; SearchFarmResultGridView.PageIndex = pageIndex; SearchFarmResultGridView.DataBind(); //Disable actions on deleted Contacts for (int i = 0; i < SearchFarmResultGridView.Rows.Count; i++) { if (farms[SearchFarmResultGridView.Rows[i].DataItemIndex].Deleted) { SearchFarmResultGridView.Rows[i].Cells[1].Enabled = false; } } if (IsPrinterRole) { SearchFarmResultGridView.Columns[0].Visible = false; SearchFarmResultGridView.Columns[1].Visible = false; DeleteButton.Visible = false; } SearchCriteriaPane.Visible = false; SearchResultPane.Visible = true; ResultCountLiteral.Text = "Farm data results - " + farms.Length + " Contacts found."; if (!IsAgentRole) { AgentListPanel.Visible = false; if (where.Contains("TBL_FARM.User_Id = ")) { int startPos = where.IndexOf("TBL_FARM.User_Id = ") + "TBL_FARM.User_Id = ".Length; int endPos = where.IndexOf(" AND "); endPos = endPos - startPos; string agentid = where.Substring(startPos, endPos); for (int i = 0; i < AgentListDropDownList.Items.Count; i++) { if (AgentListDropDownList.Items[i].Value.ToString().Equals(agentid)) { AgentListDropDownList.SelectedIndex = i; } } ForAgentLiteral.Text = AgentListDropDownList.SelectedValue + " / " + AgentListDropDownList.SelectedItem.Text; } } MessageLiteral.Text = ""; } } } catch (Exception exception) { log.Error("UNKNOWN ERROR WHILE LOADING SEARCH FARM:", exception); ErrorLiteral.Text = "UNKNOWN ERROR: Please Contact Administrator"; } } else { if ((!DeleteMessageHiddenField.Value.ToString().Equals("")) && (DeleteConformationHiddenField.Value.ToString().Equals("true"))) { // List of Contacts to be deleted List <Int64> contactIdList = new List <Int64>(); for (int i = 0; i < SearchFarmResultGridView.Rows.Count; i++) { GridViewRow row = SearchFarmResultGridView.Rows[i]; bool isChecked = ((CheckBox)row.FindControl("SelectContactCheckBox")).Checked; if (isChecked) { Int64 temp = Int64.Parse(((CheckBox)row.FindControl("SelectContactCheckBox")).ToolTip); contactIdList.Add(temp); } } try { // Get the common web service instance. ServiceAccess serviceLoader = ServiceAccess.GetInstance(); FarmService.FarmService farmService = serviceLoader.GetFarm(); for (int j = 0; j < contactIdList.Count; j++) { farmService.DeleteContact(contactIdList[j], LoginUserId); } MessageLiteral.Text = "Selected Contacts have been Deleted"; int CurrentPageIndex = SearchFarmResultGridView.PageIndex; string where = GenerateSearchCondition(); FarmService.FarmDetailsReportInfo[] farms = farmService.GetSearchFarmData(where); SearchFarmResultGridView.DataSource = farms; SearchFarmResultGridView.PageIndex = CurrentPageIndex; SearchFarmResultGridView.DataBind(); //Disable actions on deleted Contacts for (int k = 0; k < SearchFarmResultGridView.Rows.Count; k++) { if (farms[SearchFarmResultGridView.Rows[k].DataItemIndex].Deleted) { SearchFarmResultGridView.Rows[k].Cells[1].Enabled = false; } } ResultCountLiteral.Text = "Found " + farms.Length + " matching records."; } catch (Exception exception) { log.Error("UNKNOWN ERROR WHILE DELECTING CONATACTS:", exception); MessageLiteral.Text = "UNKNOWN ERROR : Please Contact Administrator"; } } DeleteMessageHiddenField.Value = ""; DeleteConformationHiddenField.Value = ""; DeleteContactIdHiddenField.Value = ""; } }
protected void DeleteContactButton_Click(object sender, EventArgs e) { // List of Contacts to be deleted List <Int64> contactIdList = new List <Int64>(); for (int i = 0; i < ContactListGridView.Rows.Count; i++) { GridViewRow row = ContactListGridView.Rows[i]; bool isChecked = ((CheckBox)row.FindControl("ContactIdCheckBox")).Checked; if (isChecked) { Int64 temp = Int64.Parse(((CheckBox)row.FindControl("ContactIdCheckBox")).ToolTip); contactIdList.Add(temp); } } if (contactIdList.Count < 1) // Atleast One Contact to be selected { MessageLiteral.Text = "Select Atleast One Contact to Delete"; } else { // Delete the List of Selected Contacts try { // Get the common web service instance. ServiceAccess serviceLoader = ServiceAccess.GetInstance(); FarmService.FarmService farmService = serviceLoader.GetFarm(); for (int i = 0; i < contactIdList.Count; i++) { farmService.DeleteContact(contactIdList[i], LoginUserId); } MessageLiteral.Text = "Selected Contact List deleted"; int plotId = 0; int.TryParse(PlotIdHiddenField.Value, out plotId); int plotContactCount = farmService.GetContactCountForPlot(plotId); // Check If Plot is Empty if (plotContactCount > 0) { ContactCountLabel.Text = plotContactCount.ToString(); FarmService.ContactInfo[] contacts = farmService.GetContactListForPlot(plotId); ContactListGridView.DataSource = contacts; ContactListGridView.DataBind(); if (farmService.IsDefaultPlot(plotId)) { DefaultContactHiddenField.Value = "true"; } else { DefaultContactHiddenField.Value = "false"; } ContactCountHiddenField.Value = contacts.Length.ToString(); } else // Action for Empty Plot is to remove the Plot { farmService.DeletePlot(plotId, LoginUserId); //Check for Empty Farm if (farmService.GetPlotCountForFarm(Convert.ToInt32(FarmIdHiddenField.Value)) > 0) { Response.Redirect("~/Members/ViewFarm.aspx?farmId=" + FarmIdHiddenField.Value.ToString()); } else // Action for Empty Farm is to remove Farm { farmService.DeleteFarm(Convert.ToInt32(FarmIdHiddenField.Value), LoginUserId); Response.Redirect("~/Members/FarmManagement.aspx"); } } } catch (Exception exception) { log.Error("UNKNOWN ERROR WHILE DELETE CONTACT:", exception); if (exception.Message.Contains("This action will cause Farm to be deleted and the Parent-Farm is Active. Hence Contact Cannot be deleted")) { MessageLiteral.Text = "This action will cause Farm to be deleted and the Parent-Farm is Active. Hence Contact Cannot be deleted"; } else { MessageLiteral.Text = "Error: Unable to Delete Contact"; } } } }