コード例 #1
0
    protected void CarsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditCar")
        {
            int      rowIndex = Convert.ToInt32(e.CommandArgument);
            CarClass cc       = new CarClass(Profile.UserName);
            int      miles    = 0;
            string   desc     = "";
            string   imgUrl   = "";

            GridViewRow myRow    = CarsGridView.Rows[rowIndex];
            TextBox     boxMiles = (TextBox)myRow.FindControl("EditMiles");
            TextBox     boxDesc  = (TextBox)myRow.FindControl("EditDesc");
            FileUpload  newImg   = (FileUpload)myRow.FindControl("FileUpload2");
            Label       myId     = (Label)myRow.FindControl("CarId");
            Guid        id       = Guid.Parse(myId.Text);

            if (boxMiles.Text != "")
            {
                miles = Convert.ToInt32(boxMiles.Text);
            }
            if (boxDesc.Text != "")
            {
                desc = boxDesc.Text;
            }

            cc.EditCarInfo(id, miles, desc);

            UserControl ucx      = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx");
            Label       txtLabel = (Label)ucx.FindControl("TextLabel");

            if (newImg.FileName != "")
            {
                string ext = System.IO.Path.GetExtension(newImg.FileName);
                if (ext == ".jpg" || ext == ".jpeg")
                {
                    string virtualPath  = "~/Images/";
                    string physicalPath = Server.MapPath(virtualPath);
                    Guid   fileName     = Guid.NewGuid();

                    newImg.SaveAs(System.IO.Path.Combine(physicalPath, fileName + ext));
                    imgUrl = System.IO.Path.Combine(virtualPath, fileName + ext);

                    if (cc.AddVehicleImage(imgUrl, id, false))
                    {
                        txtLabel.Text = "Success!";
                        Form.Controls.Add(ucx);
                    }
                    else
                    {
                        txtLabel.Text = "3 Images max.";
                        Form.Controls.Add(ucx);
                    }
                }
                else
                {
                    txtLabel.Text = "Image must be .jpg or .jpeg";
                    Form.Controls.Add(ucx);
                }
            }
            else
            {
                txtLabel.Text = "Success!";
                Form.Controls.Add(ucx);
            }

            CarsGridView.DataSourceID = "CarsDataSource";
            CarsGridView.DataBind();
        }
        if (e.CommandName == "DeleteVehicle")
        {
            using (SwapEntities myEnt = new SwapEntities())
            {
                string userName = Profile.UserName;
                string strId    = Convert.ToString(e.CommandArgument);
                Guid   veGuid   = Guid.Parse(strId);

                var checkSwaps = from tbl in myEnt.ScheduledSwaps
                                 where tbl.UserMain == userName &
                                 tbl.MainRated == false & tbl.VeMain == veGuid ||
                                 tbl.UserMain == userName &
                                 tbl.MainRated == false & tbl.VeOther == veGuid ||
                                 tbl.UserOther == userName &
                                 tbl.OtherRated == false & tbl.VeMain == veGuid ||
                                 tbl.UserOther == userName &
                                 tbl.OtherRated == false & tbl.VeOther == veGuid
                                 select tbl;

                if (checkSwaps.Count() >= 1)
                {
                    UserControl ucx      = (UserControl)LoadControl("~/Controls/UserNoticeModal.ascx");
                    Label       txtLabel = (Label)ucx.FindControl("TextLabel");
                    txtLabel.Text = "You cannot delete a vehicle with a scheduled swap pending.";
                    Form.Controls.Add(ucx);
                }
                else
                {
                    var getVe = (from vetbl in myEnt.UserVehicles
                                 where vetbl.Id == veGuid
                                 select vetbl).SingleOrDefault();

                    if (getVe != null)
                    {
                        myEnt.DeleteObject(getVe);
                        myEnt.SaveChanges();
                    }

                    var getImgs = from tbl in myEnt.VeImages
                                  where tbl.VehicleId == veGuid
                                  select tbl;

                    foreach (var item in getImgs)
                    {
                        string imgPath    = item.ImageUrl;
                        string serverPath = Server.MapPath(imgPath);
                        try
                        {
                            System.IO.File.Delete(serverPath);
                        }
                        catch
                        {
                        }
                    }

                    var validCheck = (from vetbl in myEnt.UserVehicles
                                      where vetbl.UserName == userName
                                      select vetbl).SingleOrDefault();

                    if (validCheck == null)
                    {
                        Profile.IsValidated = false;
                    }

                    Response.Redirect("~/MyProfile/EditProfile.aspx");
                }
            }
        }
    }