我有一个对象列表,它是数据绑定到网格视图的。网格视图有一个附加到该行的删除按钮。
我需要通过点击保持对象列表(就像离线工作一样)我不希望使用会话、cookie 或视图状态
当前行为:列表有 4 个项目 => 删除 1 个项目 => 列表有 3 个项目 => 删除 1 个项目(POSTBACK)列表重新生成 4 个项目,然后删除 1 个。
class Emails
{
public string Email { get; set; }
public Emails(string _Address)
{
Email = _Address;
}
}
if (!IsPostBack)
{
ListOfEmails = new List<Emails>();
ListOfEmails.Add(new Emails("1@a.com"));
ListOfEmails.Add(new Emails("2@a.com"));
ListOfEmails.Add(new Emails("3@a.com"));
ListOfEmails.Add(new Emails("4@a.com"));
GridView1.DataSource = ListOfEmails;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string EmailToBeDeleted = GridView1.Rows[e.RowIndex].Cells[0].Text;
ListOfEmails.Remove(ListOfEmails.Find(L => L.Email == EmailToBeDeleted));
GridView1.DataSource = null;
GridView1.DataSource = ListOfEmails;
GridView1.DataBind();
}
aspx:
<asp:BoundField DataField="Email" HeaderText="Email Address" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Action2"
ShowHeader="True" Text="Delete" />
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/delete.gif"
ShowDeleteButton="True" />
“我现在有 2 个删除按钮”