1

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?

8
  • 1
    How are you adding the linkbutton to the page, with just the code you have shown there will be nothing to click on. Commented Sep 17, 2014 at 14:54
  • Include the event handler which adds the LinkButton to the page. Commented Sep 17, 2014 at 14:56
  • Sorry for the confusion. I edited my post to show how the LinkButton is getting added. Commented Sep 17, 2014 at 14:59
  • Check the page lifecycle - if you're trying to add the button too late in the rendering process, it won't be there. Commented Sep 17, 2014 at 15:02
  • 1
    At least here, your code works correctly. I literally pasted your code to my project and the link is rendered and the breakpoint's hit when _lb_Click is clicked. I create the LinkButton in Page_Load. Commented Sep 17, 2014 at 15:03

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

From the post, you mention that the LinkButton is being rendered, so I assume that the page is being rendered correctly. Have you tried just adding the event handler (ie. _lb.Click += _lb_Click;) in Page_Load?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.