예제 #1
0
        protected void EditOrCreateCategotyButton_Click(object sender, EventArgs e)
        {
            var    dbContext    = new BooksReviewContext();
            string categoryName = this.EditOrCreateCategotyTextBox.Text;
            Button button       = sender as Button;

            if (button.CommandName == "edit")
            {
                int id       = Convert.ToInt32(button.CommandArgument);
                var category = dbContext.Categories.Find(id);
                category.Name = categoryName;
            }
            else if (button.CommandName == "delete")
            {
                int id       = Convert.ToInt32(button.CommandArgument);
                var category = dbContext.Categories.Find(id);
                var books    = category.Books.ToList();
                dbContext.Books.RemoveRange(books);
                dbContext.Categories.Remove(category);
            }
            else
            {
                var category = new Category {
                    Name = categoryName
                };
                dbContext.Categories.Add(category);
            }

            try
            {
                dbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                this.ErrorLabel.Text = ex.Message;
            }
            this.GridViewCategories.DataBind();
            this.EditOrCreateCategotyPanel.Visible = false;
        }
        protected void EditOrCreateBook_Click(object sender, EventArgs e)
        {
            var dbContext = new BooksReviewContext();

            Button button = sender as Button;

            if (button.CommandName == "edit")
            {
                int id   = Convert.ToInt32(button.CommandArgument);
                var book = dbContext.Books.Find(id);
                book.Title       = this.TitleTextBox.Text = book.Title;
                book.Author      = this.AuthorTextBox.Text;
                book.ISBN        = this.ISBNTextBox.Text;
                book.SiteURL     = this.UrlTextBox.Text;
                book.Description = this.DescrTextBox.Text;
                book.CategoryId  = Convert.ToInt32(this.CategoryDropDownList.SelectedValue);
            }
            else if (button.CommandName == "delete")
            {
                int id   = Convert.ToInt32(button.CommandArgument);
                var book = dbContext.Books.Find(id);
                dbContext.Books.Remove(book);
            }
            else
            {
                var book = new Book();
                book.Title       = this.TitleTextBox.Text;
                book.Author      = this.AuthorTextBox.Text;
                book.ISBN        = this.ISBNTextBox.Text;
                book.SiteURL     = this.UrlTextBox.Text;
                book.Description = this.DescrTextBox.Text;
                book.CategoryId  = Convert.ToInt32(this.CategoryDropDownList.SelectedValue);
                dbContext.Books.Add(book);
            }
            dbContext.SaveChanges();
            this.GridViewBooks.DataBind();
            this.BookPanel.Visible = false;
        }