Dynamic button onclick issue
I have the following piece of code:
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);
}
Now for some reason, which i can't seem to figure out that when ever i
click on any of the buttons, the number that is passed which is in
variable "num" is always 5. Does anyone know why this happens? Or why it
doesn't assign the actual number that it should be to "num"?
No comments:
Post a Comment