Пример #1
0
        protected void AddArticleButton_Click(object sender, EventArgs e)
        {
            Boolean fileOK = false;
            String  path   = Server.MapPath("~/Images/");

            if (ArticleImage.HasFile)
            {
                String   fileExtension     = System.IO.Path.GetExtension(ArticleImage.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.
                    ArticleImage.PostedFile.SaveAs(path + ArticleImage.FileName);
                    // Save to Images/Thumbs folder.
                    ArticleImage.PostedFile.SaveAs(path + "Thumbs/" + ArticleImage.FileName);
                }
                catch (Exception ex)
                {
                    LabelAddStatus.Text = ex.Message;
                }

                // Add product data to DB.
                AddArticles articles   = new AddArticles();
                bool        addSuccess = articles.AddArticle(AddArticleName.Text, AddArticleDescription.Text, AddArticleDate.SelectedDate,
                                                             DropDownAddCategory.SelectedValue, ArticleImage.FileName);
                if (addSuccess)
                {
                    // Reload the page.
                    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                    Response.Redirect(pageUrl + "?ArticleAction=add");
                }
                else
                {
                    LabelAddStatus.Text = "Unable to add new article to database.";
                }
            }
            else
            {
                LabelAddStatus.Text = "Unable to accept file type.";
            }
        }
        protected void DetailsView1_RowCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToArticles")
            {
                string myArticleName = ProposedArticlesDetails.Rows[1].Cells[1].Text;
                string myDescription = ProposedArticlesDetails.Rows[2].Cells[1].Text;
                string myImagePath   = ProposedArticlesDetails.Rows[4].Cells[1].Text;
                string myCategoryID  = ProposedArticlesDetails.Rows[5].Cells[1].Text;

                AddArticles articles   = new AddArticles();
                bool        addSuccess = articles.AddArticle(myArticleName, myDescription, DateTime.UtcNow,
                                                             myCategoryID, myImagePath);
                if (addSuccess)
                {
                    LabelAddStatus.Text = "Article proposed by user had been added in database.";
                    ProposedArticlesDetails.DeleteItem();
                }
                else
                {
                    LabelAddStatus.Text = "Unable to add new article to database.";
                }
            }
        }