I am trying to delete a selected item from the drop down list using C#, however I just can not get it to work. Can anyone show me where I am going wrong? I have tried the RemoveAll and RemoveAt tags (which I think i correct) but I may be placing them in the wrong place? Thanks.
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;
}
}
I'm not sure what the problem is, and why exactly you are trying to retreive the book that you are deleting, BUT every time you make changes to the datasource, you need to rebind it to the control.
Try repeating the following block in btnDelete_Click
ddlDelete.DataSource = catalogueInstance.books;
ddlDelete.DataTextField = "title";
ddlDelete.DataValueField = "id";
Another possible problem is that since you are not performing the initial binding when !Page.IsPostback()
, you are actually rebinding the datasource before the delete event. Remember, Page_Load
is always the first event after a postback.
EDIT
You are trying to use Catalogue catalogueInstance
as a global variable and then on page load read the same file over and over again.
Viewstate
so that the state of the global variable persists the various page refreshes that will occur.Try the following, simpler solution and I'm sure you'll see the problem:
//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;
}
You should use IsPostBack
in 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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.