从列表中删除所有项目后的表格显示消息

时间:2018-05-16 09:43:20

标签: c# asp.net

我正在尝试从下拉列表中填充的列表中删除项目,并希望在删除所有项目后显示一条消息,表明列表中没有项目。 注释掉的线条应该可以使它工作但我似乎无法让我正确。 如果有人能提供帮助,那将不胜感激。

提前致谢。

public partial class DeleteBook : System.Web.UI.Page
{
public Catalogue catalogueInstance = new Catalogue();

//Filepath for json file
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);
    // reading data contained in the json filepath
    //convert objects in json file to lists
    catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);

    if (IsPostBack) return;

    ddlDelete.DataSource = catalogueInstance.books;
    ddlDelete.DataTextField = "title";
    ddlDelete.DataValueField = "id";

    ddlDelete.Items.Insert(0, "There are no items in the Catalogue to 
Display");

    //binding the data to Drop Down List
    ddlDelete.DataBind();
}

protected void ddlDelete_SelectedIndexChanged(object sender, EventArgs e)
{

    string jsonText = File.ReadAllText(FILENAME);
    // reading data contained in the json filepath
    //convert objects in json file to lists
    catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);

    Book b = catalogueInstance.books[ddlDelete.SelectedIndex - 1];
    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)
{

    // Get the catalogue instance again (gone stale)
    Catalogue catalogueInstance = new Catalogue();
    // get the book id.
    ddlDelete.DataSource = catalogueInstance.books;
    // Get the book.

    Book b = catalogueInstance.books[ddlDelete.SelectedIndex - 1];
    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;

    // Delete the book from catalogue instance
    catalogueInstance.books.RemoveAt(ddlDelete.SelectedIndex - 1);

    ddlDelete.SelectedIndex = 0;

    // serialise and write catalogue instance to file.
    catalogueInstance.books.RemoveAt(ddlDelete.SelectedIndex - 1);

    ddlDelete.SelectedIndex = 0;

    // rebind the ddl.
    ddlDelete.DataBind();

    int id = Int32.Parse(txtID.Text);
    Book book = catalogueInstance.books.SingleOrDefault(b => b.id == id);
    //catalogueInstance.books.Remove(book);

    catalogueInstance.books.RemoveAt(ddlDelete.SelectedIndex - 1);

    ddlDelete.SelectedIndex = 0;


    //ddlDelete_SelectedIndexChanged(ddlDelete, new EventArgs());

    if (book != null)
    {

        book.title = txtTitle.Text;
        book.year = Int32.Parse(txtYear.Text);
        book.author = txtAuthor.Text;
        book.publisher = txtPublisher.Text;
        book.isbn = txtISBN.Text;

        string jsonText = JsonConvert.SerializeObject(catalogueInstance);
        File.WriteAllText(FILENAME, jsonText);
    }
    txtSummary.Text = "Book ID of " + id + " has Been deleted from the 
Catalogue" + Environment.NewLine;

    if (!catalogueInstance.books.Any())
    {
        txtSummary.Text = "There are no items in the Catalogue";
    }

}
}

1 个答案:

答案 0 :(得分:0)

我认为永远不会抛出异常,因为您的文件始终存在。从文件中删除所有项目并不意味着该文件也将被删除。您需要稍微更改一下代码。您可以检查catalogueInstance.books,它没有任何书籍或检查File.ReadAllText(FILENAME);.我会检查1:

if(!catalogueInstance.books.Any()){
    txtSummary.Text = "There are no items in the Catalogue";
}

所以你可以删除你的try-catch块。