Пример #1
0
    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";
                }
            }
        }
    }