【发布时间】:2016-04-29 12:03:09
【问题描述】:
我正在尝试将一些自定义对象存储到一个列表中,并通过会话在回发之间存储它。但由于某种原因,列表没有更新以显示在 Debug 或 GridView 上。
List<Book> booklist;
protected void Page_Load(object sender, EventArgs e) {
if (Session["InitialBooklist"] == null) {
booklist = new List<Book>();
Session.Add("InitialBooklist", booklist);
} else {
booklist = Session["InitialBooklist"] as List<Book>;
GridView1.DataSource = booklist;
GridView1.DataBind();
foreach (Book book in booklist) {
System.Diagnostics.Debug.Write(book.ToString());
}
}
}
这是将按钮添加到表中的一行(与 GridView 不同)并为其提供单击处理程序的方法的适当部分...
TableCell addButtonCell = new TableCell();
Button addButton = new Button();
addButton.Text = "Add";
addButton.Click += delegate(object sender1, EventArgs e1) {
addButton_Click(sender1, e1, title, author, price, coverURL, ISBN, numPages, amazonURL);
};
addButtonCell.Controls.Add(addButton);
row.Cells.Add(addButtonCell);
最后,这是点击处理程序...
public void addButton_Click(object sender, EventArgs e, String title, String author, String price, String coverURL, String ISBN, String numPages, String amazonURL) {
booklist.Add(new Book(title, author, price, coverURL, ISBN, numPages, amazonURL));
}
【问题讨论】:
-
看起来您的编码错误。为什么不检查 isPostBack Page_Load 事件?如果您不检查 isPostBack,您的书单将始终为空。