I have dynamically added a linkbutton to the page. But the click event is not working. What could be the problem? Thanks for your help. I am adding a linkbutton on button click event.
Here is my code.
protected void Button1_Click(object sender, EventArgs e)
{
LinkButton lb = new LinkButton();
lb.Text = "dsadsa";
lb.ID = "22";
lb.CommandArgument = "22";
lb.CommandName = "22";
lb.Command += new CommandEventHandler(lb1_Command);
PlaceHolder1.Controls.Add(lb);
}
protected void lb1_Command(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandName;
}
the linkbutton isn’t recreated when the linkbutton is clicked thus you’re event handler isn’t registered thus you’re event doesn’t get fired.
Adding a button in an eventhandler is almost always a bad idea, you can add this by default on the page and just set it to
Button.Visible = false. This way you can register your eventhandler earlier in sayPage_Loadand set it visible from the event handler.