protected void Edit_Click(object sender, EventArgs e) { GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow; var bookId = int.Parse(BooksGrid.DataKeys[row.RowIndex].Value.ToString()); LibrarySystemDbContext data = new LibrarySystemDbContext(); var book = data.Books.FirstOrDefault(x => x.Id == bookId); BookEdit.DataSource = new List <Book> { book }; BookEdit.DataBind(); (BookEdit.FindControl("BookCategory") as DropDownList).SelectedValue = book.CategoryId.ToString(); }
protected void SaveEdit_Click(object sender, EventArgs e) { var title = (BookEdit.FindControl("BookTitle") as TextBox).Text; var authors = (BookEdit.FindControl("BookAuthors") as TextBox).Text; var ISBN = (BookEdit.FindControl("BookIsbn") as TextBox).Text; var site = (BookEdit.FindControl("BookSite") as TextBox).Text; var description = (BookEdit.FindControl("BookDescription") as TextBox).Text; var categoryId = int.Parse((BookEdit.FindControl("BookCategory") as DropDownList).SelectedValue); if (string.IsNullOrEmpty(title)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty"); return; } else if (title.Length < 5 || title.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title must be between 5 and 50 symbols"); return; } else if (string.IsNullOrEmpty(authors)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } else if (authors.Length < 5 || authors.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors must be between 5 and 50 symbols"); return; } else if (ISBN.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (site.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (description.Length > 2000) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Description must be less than 2000 symbols"); return; } else if (categoryId == -1 || categoryId == null) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } var id = int.Parse(BookEdit.DataKey.Value.ToString()); LibrarySystemDbContext data = new LibrarySystemDbContext(); var book = data.Books.FirstOrDefault(x => x.Id == id); book.Title = title; book.Authors = authors; book.ISBN = ISBN; book.WebSite = site; book.Description = description; book.CategoryId = categoryId; data.SaveChanges(); BookEdit.DataSource = null; BookEdit.DataBind(); BooksGrid.DataBind(); }