示例#1
0
        /// <summary>
        /// Behavior for adding product
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddProductButton_Click(object sender, EventArgs e)
        {
            bool fileOK = false;

            String path = Server.MapPath("~/Catalog/Images/");

            if (ProductImage.HasFile)
            {
                String   fileExtension     = System.IO.Path.GetExtension(ProductImage.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }

            if (fileOK)
            {
                try
                {
                    // Save to Images folder.
                    ProductImage.PostedFile.SaveAs(path + ProductImage.FileName);
                    // Save to Images/Thumbs folder.
                    ProductImage.PostedFile.SaveAs(path + "Thumbs/" + ProductImage.FileName);
                }
                catch (Exception ex)
                {
                    lblAddStatus.Text = ex.Message;
                }

                // Add product data to DB.

                bool addSuccess = m_adminEngine.AddProduct(AddProductName.Text, AddProductDescription.Text,
                                                           AddProductPrice.Text, DropDownAddCategory.SelectedValue, ProductImage.FileName);
                if (addSuccess)
                {
                    // Reload the page.
                    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                    Response.Redirect(pageUrl + "?ProductAction=add");
                }
                else
                {
                    lblAddStatus.Text = "Unable to add new product to database.";
                }
            }
            else
            {
                lblAddStatus.Text = "Unable to accept file type.";
            }
        }