Exemplo n.º 1
0
    protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "PerformInsert")
            {
                var item          = e.ListViewItem;
                var FileImageName = (RadUpload)item.FindControl("FileImageName");

                var strProductName          = ((Label)FormView1.FindControl("lblProductName")).Text.Trim();
                var strConvertedProductName = Common.ConvertTitle(strProductName);
                var strImageName            = FileImageName.UploadedFiles.Count > 0 ? Guid.NewGuid().GetHashCode().ToString("X") + FileImageName.UploadedFiles[0].GetExtension() : "";
                var strTitle         = ((TextBox)item.FindControl("txtTitle")).Text.Trim();
                var strDescription   = ((TextBox)item.FindControl("txtDescription")).Text.Trim();
                var strTitleEn       = ((TextBox)item.FindControl("txtTitleEn")).Text.Trim();
                var strDescriptionEn = ((TextBox)item.FindControl("txtDescriptionEn")).Text.Trim();
                var IsAvailable      = ((CheckBox)item.FindControl("chkAddIsAvailable")).Checked.ToString();
                var Priority         = ((RadNumericTextBox)item.FindControl("txtPriority")).Text.Trim();
                var oProductImage    = new ProductImage();

                oProductImage.ProductImageInsert(
                    strImageName,
                    strConvertedProductName,
                    strTitle,
                    strDescription,
                    strTitleEn,
                    strDescriptionEn,
                    Request.QueryString["PI"],
                    IsAvailable,
                    Priority);

                string strFullPath = "~/res/product/album/" + strImageName;

                if (!string.IsNullOrEmpty(strImageName))
                {
                    FileImageName.UploadedFiles[0].SaveAs(Server.MapPath(strFullPath));
                    ResizeCropImage.ResizeByCondition(strFullPath, 700, 445);
                    ResizeCropImage.CreateThumbNailByCondition("~/res/product/album/", "~/res/product/album/thumbs/", strImageName, 120, 120);
                }
                RadListView1.InsertItemPosition = RadListViewInsertItemPosition.None;
            }
            else if (e.CommandName == "Update")
            {
                var item          = e.ListViewItem;
                var FileImageName = (RadUpload)item.FindControl("FileImageName");
                var dsUpdateParam = ObjectDataSource1.UpdateParameters;

                var strProductImageID       = ((HiddenField)e.ListViewItem.FindControl("hdnProductImageID")).Value;
                var strProductName          = ((Label)FormView1.FindControl("lblProductName")).Text.Trim();
                var strConvertedProductName = Common.ConvertTitle(strProductName);
                var strOldImageName         = ((HiddenField)e.ListViewItem.FindControl("hdnImageName")).Value;
                var strIsAvailable          = ((CheckBox)item.FindControl("chkAddIsAvailable")).Checked.ToString();
                var strImageName            = FileImageName.UploadedFiles.Count > 0 ? Guid.NewGuid().GetHashCode().ToString("X") + FileImageName.UploadedFiles[0].GetExtension() : "";

                dsUpdateParam["ImageName"].DefaultValue            = !string.IsNullOrEmpty(strImageName) ? strImageName : strOldImageName;
                dsUpdateParam["ConvertedProductName"].DefaultValue = strConvertedProductName;
                dsUpdateParam["IsAvailable"].DefaultValue          = strIsAvailable;

                if (!string.IsNullOrEmpty(strImageName))
                {
                    var strOldImagePath      = Server.MapPath("~/res/product/album/" + strOldImageName);
                    var strOldThumbImagePath = Server.MapPath("~/res/product/album/thumbs/" + strOldImageName);

                    if (File.Exists(strOldImagePath))
                    {
                        File.Delete(strOldImagePath);
                    }
                    if (File.Exists(strOldThumbImagePath))
                    {
                        File.Delete(strOldThumbImagePath);
                    }

                    strImageName = (string.IsNullOrEmpty(strConvertedProductName) ? "" : strConvertedProductName + "-") + strProductImageID + strImageName.Substring(strImageName.LastIndexOf('.'));
                    string strFullPath = "~/res/product/album/" + strImageName;

                    FileImageName.UploadedFiles[0].SaveAs(Server.MapPath(strFullPath));
                    ResizeCropImage.ResizeByCondition(strFullPath, 700, 445);
                    ResizeCropImage.CreateThumbNailByCondition("~/res/product/album/", "~/res/product/album/thumbs/", strImageName, 120, 120);
                }
            }
            else if (e.CommandName == "Delete")
            {
                var strOldImageName = ((HiddenField)e.ListViewItem.FindControl("hdnImageName")).Value;
                DeleteImage(strOldImageName);
            }
            else if (e.CommandName == "QuickUpdate")
            {
                string ProductImageID, Priority, IsAvailable;
                var    oProductImage = new ProductImage();

                foreach (RadListViewDataItem item in RadListView1.Items)
                {
                    ProductImageID = item.GetDataKeyValue("ProductImageID").ToString();
                    Priority       = ((RadNumericTextBox)item.FindControl("txtPriority")).Text.Trim();
                    IsAvailable    = ((CheckBox)item.FindControl("chkIsAvailable")).Checked.ToString();

                    oProductImage.ProductImageQuickUpdate(
                        ProductImageID,
                        IsAvailable,
                        Priority
                        );
                }
            }
            else if (e.CommandName == "DeleteSelected")
            {
                var    oProductImage = new ProductImage();
                string ProductImageID, OldImageName;

                foreach (RadListViewDataItem item in RadListView1.Items)
                {
                    var chkSelect = (CheckBox)item.FindControl("chkSelect");

                    if (chkSelect.Checked)
                    {
                        ProductImageID = item.GetDataKeyValue("ProductImageID").ToString();
                        OldImageName   = ((HiddenField)item.FindControl("hdnImageName")).Value;

                        DeleteImage(OldImageName);
                        oProductImage.ProductImageDelete(ProductImageID);
                    }
                }
            }
            RadListView1.Rebind();
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }