我正在尝试使用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;
}
}
答案 0 :(得分:0)
我不确定问题是什么,为什么你正在试图检索你要删除的书,但每次你对数据源进行更改时,你需要重新绑定它控制。
尝试在btnDelete_Click
ddlDelete.DataSource = catalogueInstance.books;
ddlDelete.DataTextField = "title";
ddlDelete.DataValueField = "id";
另一个可能的问题是,由于您在!Page.IsPostback()
时未执行初始绑定,因此实际上是在删除事件之前重新绑定数据源 。请注意,Page_Load
始终是回发后的第一个事件。
修改强>
您正在尝试将Catalogue catalogueInstance
用作全局变量,然后在页面加载时反复读取同一文件。
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;
}
答案 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();
}
}