C# 从列表C中删除项目

C# 从列表C中删除项目,c#,asp.net,C#,Asp.net,我正在尝试使用C从下拉列表中删除所选项目,但是我无法使其正常工作。谁能告诉我哪里出了问题? 我已经尝试了RemoveAll和RemoveAt标签,我认为我是正确的,但我可能把它们放在了错误的地方? 谢谢 我不知道问题出在哪里,也不知道为什么您要尝试检索正在删除的书籍,但每次更改数据源时,都需要将其重新绑定到控件 尝试在btnDelete\u单击中重复以下块 另一个可能的问题是,因为您没有执行初始绑定!实际上,您正在删除事件之前重新绑定数据源。请记住,页面加载始终是回发后的第一个事件 编辑 您试图

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


我不知道问题出在哪里,也不知道为什么您要尝试检索正在删除的书籍,但每次更改数据源时,都需要将其重新绑定到控件

尝试在btnDelete\u单击中重复以下块

另一个可能的问题是,因为您没有执行初始绑定!实际上,您正在删除事件之前重新绑定数据源。请记住,页面加载始终是回发后的第一个事件

编辑

您试图使用Catalog CatalogeInstance作为全局变量,然后在页面加载时反复读取同一文件

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

//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;
}
您应该在页面加载中使用IsPostBack:


您当前的解决方案有什么问题?您没有检查Page_load方法中的Page.IsPostBack属性,因为每次单击delete按钮都会再次生成下拉列表,从而触发回发。这只是问题的一部分。他没有在删除事件后重新绑定数据源我想回发是这里的主要问题。。。我更喜欢在删除元素而不是使用索引时删除所有元素。我现在收到以下错误:在App_Web_0zdujtuy.dll中发生了类型为“System.NullReferenceException”的异常,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。当页面到达此处时运行页面:ddlDelete.DataSource=catalogeinstance.books;查看我的编辑
ddlDelete.DataSource = catalogueInstance.books;
ddlDelete.DataTextField = "title";
ddlDelete.DataValueField = "id";
//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;
}
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();
    }
}