我在以编程方式添加控件时遇到问题,Panel
其中控件在循环内有事件。这是我的代码:
pnlAccompanies.Controls.Clear();
foreach (var accompany in this.RetrieveAccompanyList())
{
var btnRemoveAccompany = new LinkButton();
btnRemoveAccompany.CommandArgument = accompany.AccompanyID.ToString();
btnRemoveAccompany.CssClass = "close";
btnRemoveAccompany.Controls.Add(new Literal() { Text = "×" });
btnRemoveAccompany.Click += this.btnRemoveAccompany_Click;
smGTPForm.RegisterAsyncPostBackControl(btnRemoveAccompany);
pnlAccompanies.Controls.Add(btnRemoveAccompany);
}
这是我的事件代码:
protected void btnRemoveAccompany_Click(object sender, EventArgs e)
{
var accompanyID = ((LinkButton)sender).CommandArgument.Parser<int>();
var accompanies = this.RetrieveAccompanyList();
if (accompanies.Exists(o => o.AccompanyID == accompanyID))
{
accompanies.RemoveAll(o => o.AccompanyID == accompanyID);
HttpContext.Current.Session["gtpd_accompany01"] = accompanies;
this.PopulateAccompanyList();
}
}
accompanies
当我在. ((LinkButton)sender).CommandArgument
_ ((LinkButton)sender).CommandArgument
为什么会这样?
我的代码在 C# 4.0、ASP.NET 中,在 VS2010 Pro 中构建。
请帮忙。提前致谢。