我有以下代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 5; i++)
{
Button btn = new Button();
btn.ID = "button-" + i.ToString();
btn.Text = "This is button-" + i.ToString();
btn.Click += (senders, es) => test(i, PlaceHolder1, btn.ID);
PlaceHolder1.Controls.Add(btn);
}
}
protected void test(int num, PlaceHolder ph, string btnID)
{
Response.Write("The Button clicked on is: " + num);
}
现在由于某种原因,我似乎无法弄清楚当我点击任何按钮时,变量“num”中传递的数字总是5。有谁知道为什么会发生这种情况?或者为什么它没有将它应该是的实际数字分配给“num”?