实现列表所有项删除后显示提示消息的技术求助
Hey there! Let's fix up your delete functionality step by step. I noticed several issues in your btnDelete_Click method and a small tweak needed in Page_Load to get the empty list message working correctly.
Key Issues in Your Current Code
- You're creating an empty
Catalogueinstead of loading existing data: When you initialize a newCatalogueinbtnDelete_Click, you're working with an empty list instead of reading the latest books from your JSON file. - Duplicate & messy deletion logic: You're calling
RemoveAt()multiple times, which will cause errors (like trying to delete from an already modified index) and unnecessary operations. - Incorrect operation order: The empty list check runs before you've properly saved the updated data, and you're trying to modify a book right before deleting it (which is redundant).
- Dropdown initialization bug: Your "no items" message gets overwritten when
DataBind()runs because you inserted it before binding the data.
Fixed Code
First, update your Page_Load method to properly handle the empty state and preserve the default message:
public partial class DeleteBook : System.Web.UI.Page { public Catalogue catalogueInstance = new Catalogue(); const string FILENAME = @"C:\Users\tstra\Desktop\19456932_CSE2ICX_Assessment_3\Bin\Books.json"; protected void Page_Load(object sender, EventArgs e) { string jsonText = File.ReadAllText(FILENAME); catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText); if (IsPostBack) return; // Keep our default item when binding data ddlDelete.AppendDataBoundItems = true; // Add empty state item first ddlDelete.Items.Insert(0, new ListItem("There are no items in the Catalogue to Display", "-1")); ddlDelete.DataSource = catalogueInstance.books; ddlDelete.DataTextField = "title"; ddlDelete.DataValueField = "id"; ddlDelete.DataBind(); // Disable controls if there are no books if (!catalogueInstance.books.Any()) { ddlDelete.Enabled = false; btnDelete.Enabled = false; } }
Then fix the btnDelete_Click method with clean, reliable deletion logic:
protected void ddlDelete_SelectedIndexChanged(object sender, EventArgs e) { if (ddlDelete.SelectedValue == "-1") { // Clear text boxes if default item is selected txtID.Text = ""; txtTitle.Text = ""; txtAuthor.Text = ""; txtYear.Text = ""; txtPublisher.Text = ""; txtISBN.Text = ""; return; } string jsonText = File.ReadAllText(FILENAME); catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText); int selectedId = int.Parse(ddlDelete.SelectedValue); Book b = catalogueInstance.books.SingleOrDefault(book => book.id == selectedId); if (b != null) { txtID.Text = b.id.ToString(); txtTitle.Text = b.title; txtAuthor.Text = b.author; txtYear.Text = b.year.ToString(); txtPublisher.Text = b.publisher; txtISBN.Text = b.isbn; } } protected void btnDelete_Click(object sender, EventArgs e) { // Load latest catalogue data from JSON string jsonText = File.ReadAllText(FILENAME); catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText); // Check if default item is selected if (ddlDelete.SelectedValue == "-1") { txtSummary.Text = "No book selected to delete."; return; } int selectedId = int.Parse(ddlDelete.SelectedValue); Book bookToDelete = catalogueInstance.books.SingleOrDefault(b => b.id == selectedId); if (bookToDelete == null) { txtSummary.Text = "Book not found in catalogue."; return; } // Delete the book catalogueInstance.books.Remove(bookToDelete); // Save updated catalogue to JSON string updatedJson = JsonConvert.SerializeObject(catalogueInstance); File.WriteAllText(FILENAME, updatedJson); // Rebuild dropdown list ddlDelete.Items.Clear(); ddlDelete.AppendDataBoundItems = true; ddlDelete.Items.Insert(0, new ListItem("There are no items in the Catalogue to Display", "-1")); if (catalogueInstance.books.Any()) { ddlDelete.DataSource = catalogueInstance.books; ddlDelete.DataTextField = "title"; ddlDelete.DataValueField = "id"; ddlDelete.DataBind(); ddlDelete.Enabled = true; btnDelete.Enabled = true; txtSummary.Text = $"Book ID {selectedId} has been deleted from the Catalogue"; } else { // Empty state: show message and disable controls ddlDelete.Enabled = false; btnDelete.Enabled = false; txtSummary.Text = "There are no items in the Catalogue"; } // Clear text boxes after deletion txtID.Text = ""; txtTitle.Text = ""; txtAuthor.Text = ""; txtYear.Text = ""; txtPublisher.Text = ""; txtISBN.Text = ""; } }
What Changed & Why
- Reliable data loading: We always read the latest JSON data before making changes, so we never work with stale or empty data.
- Safe selection handling: We check for the default dropdown item (value
-1) to avoid invalid deletion attempts. - ID-based deletion: Using
SingleOrDefaultto find books by ID is more reliable than index positions (which shift after deletions). - Proper dropdown management: We rebuild the dropdown after each deletion, ensuring the empty state message shows correctly when no books are left. We also disable controls in empty state to prevent user errors.
- Clear feedback: We update
txtSummarywith specific messages for success, no selection, or missing book cases, and clear text boxes after deletion.
内容的提问来源于stack exchange,提问作者Strato
相关产品推荐
相关产品推荐

