コード例 #1
0
        protected void btnAddProduct_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.ToString();
            string excerpt = txtExcerpt.Text.ToString();
            string description = txtDescription.Text.ToString();
            int cost = Convert.ToInt32(txtCost.Text);
            string inStock = radInStock.SelectedValue;
            string featured = radFeatured.SelectedValue;
            string status = radStatus.SelectedValue;

            string[] allowedExtensions = { ".JPG", ".PNG" };
            string imageExtension = Path.GetExtension(fileImage.PostedFile.FileName).ToUpper();

            bool error = false;

            if (!allowedExtensions.Contains(imageExtension))
            {
                error = true;
                litError.Text = "Invalid Image Provided";
            }

            if (error)
                return;

            string image = Guid.NewGuid().ToString() + imageExtension;

            string uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "uploads");
            string uploadedFile = Path.Combine(uploadDirectory, image);

            fileImage.PostedFile.SaveAs(uploadedFile);

            Classes.Product objProduct = new Classes.Product();

            if (objProduct.addProduct(name, excerpt, description, cost, inStock, featured, status, image))
            {
                litError.Text = "Product Added Successfully";

                int productId = objProduct.getProductIdByName(name);

                foreach (ListItem category in lstCategory.Items)
                {
                    if (category.Selected)
                        objProduct.mapProductToCategory(productId, Convert.ToInt32(category.Value));
                }
            }
        }