从列表C#中删除项目

时间:2018-05-15 12:29:17

标签: c# asp.net

我正在尝试使用C#从下拉列表中删除所选项目,但是我无法让它工作。有谁能告诉我哪里出错了? 我已经尝试了RemoveAll和RemoveAt标签(我认为我更正),但我可能会把它们放在错误的地方? 感谢。

public partial class DeleteBook : System.Web.UI.Page
{
Catalogue catalogueInstance;

protected void btnDelete_Click(object sender, EventArgs e)
{
    int id = Int32.Parse(txtID.Text);
    Book book = catalogueInstance.books.RemoveAt(b => b.id == id);
    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;
}
}

2 个答案:

答案 0 :(得分:0)

我不确定问题是什么,为什么你正在试图检索你要删除的书,每次你对数据源进行更改时,你需要重新绑定它控制。

尝试在btnDelete_Click

中重复以下块
ddlDelete.DataSource = catalogueInstance.books;
ddlDelete.DataTextField = "title";
ddlDelete.DataValueField = "id";

另一个可能的问题是,由于您在!Page.IsPostback()时未执行初始绑定,因此实际上是在删除事件之前重新绑定数据源 。请注意,Page_Load 始终是回发后的第一个事件。

修改

您正在尝试将Catalogue catalogueInstance用作全局变量,然后在页面加载时反复读取同一文件。

  1. asp.net上的全局变量不能像在win表单上使用的那样使用。您需要查看Viewstate,以便全局变量的状态持续将发生的各种页面刷新。
  2. 每次刷新页面时,都会不断从文件中重新加载目录,从而替换已删除的行。请记住,您只是删除了目录中的项目,但它们仍然存在于文件中。
  3. 尝试以下更简单的解决方案,我相信您会看到问题:

    //Filepath for json file
    // this can stay here
    const string FILENAME = 
    @"C:\Users\tstra\Desktop\19456932_CSE2ICX_Assessment_3\Bin\Books.json";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        // this needs stay in here, enclosed within `!Page.IsPostback()`
        if (!Page.IsPostback()) 
        {
            Catalogue catalogueInstance;
            // reading data contained in the json filepath
            string jsonText = File.ReadAllText(FILENAME);
    
            //convert objects in json file to lists
            catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);
    
            // save the current state into ViewState
            ViewState["catalogueInstance"] = catalogueInstance;
    
            ddlDelete.DataSource = catalogueInstance.books;
            ddlDelete.DataTextField = "title";
            ddlDelete.DataValueField = "id";
    
            //binding the data to Drop Down List
            ddlDelete.DataBind();
       }
    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        // get catalogueInstance back from ViewState before trying to use it
        Catalogue catalogueInstance = (Catalogue)ViewState["catalogueInstance"];
    
        int id = Int32.Parse(txtID.Text);
        Book book = catalogueInstance.books.RemoveAt(b => b.id == id);
        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);
    
            // you need to reset, and rebind the datasource again
            ddlDelete.DataSource = catalogueInstance.books;
            ddlDelete.DataTextField = "title";
            ddlDelete.DataValueField = "id";
    
            //binding the data to Drop Down List
            ddlDelete.DataBind();
        }
        txtSummary.Text = "Book ID of " + id + " has Been deleted from the 
        Catalogue" + Environment.NewLine;
    }
    

答案 1 :(得分:0)

您应该在IsPostBack中使用Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // reading data contained in the json filepath
        string jsonText = File.ReadAllText(FILENAME);

        //convert objects in json file to lists
        catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);

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

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