简体   繁体   中英

ASP.NET Dynamically Generated Linkbutton Click event not firing

I have a panel with its visibility set to false. What I'm trying to do is dynamically create a LinkButton when my page is generated and have it so when I click the LinkButton, the panels' visibility gets set to true and it appears on my page.

I'm creating my LinkButton like so:

LinkButton _lb = new LinkButton();
_lb.Text = "Details";
_lb.Click += _lb_Click;

Here is the code for my LinkButton's Click event handlers:

protected void _lb_Click(object sender, EventArgs e)
{
      Panel1.Visible = true;
}

Whenever I render the page and click on the LinkButton, the event never fires. I can put a break point inside the event handler and it never gets reached. What am I missing?

EDIT:

I am adding the LinkButton to the page in a placeholder like so:

PlaceHolder1.Controls.Add(_lb);

EDIT2:

Thank you all for the replies so far. It seems like the problem is definitely to do with the lifecycle. To add details, the LinkButton is being created inside of a Timer_Tick event that is controlling the updating of an UpdatePanel. I don't know how to create the LinkButton in Page_Load and pass it to the Timer_Tick event. Any help?

Since you don't mention where it is, move your code to Page_Load :

protected void Page_Load(object sender, System.EventArgs e)
{
    LinkButton _lb = new LinkButton();
    _lb.Text = "Details";
    _lb.Click += _lb_Click;
    PlaceHolder1.Controls.Add(_lb);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM