How to raise an event for a linkbutton created programmatically

I am trying to add a click or onCommand event for a link button that is generated dynamically in the code behind.  I can't get it to work and I have tried many examples found on the internet.  In the example below it works if I define the linkbutton in the aspx page, but you will see below when I do it in the page load of the code behind it doesn't fire.  What am I doing wrong?  Thanks!


<asp:Panel ID="jobType2Panel" runat="server">
                <asp:LinkButton id="lb2" text="in aspx page" runat="server" OnClick="LinkButton2_Click"</asp:LinkButton>
</asp:Panel>

<asp:Label id="Label2" runat="server" />


code behind:

 protected void Page_Load(object sender, EventArgs e)
    {

        LinkButton lb3 = new LinkButton();
        lb3.OnClientClick = "LinkButton2_Click";
        lb3.Text = "in code behind";
        jobType2Panel.Controls.Add(lb3);

       
     
    }
   public void LinkButton2_Click(Object sender, EventArgs e)
    {
        Label2.Text = "You clicked the link button";
    }
farmingtonisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
OnClientClick runs JavaScript on the client when clicked, you want want:
lb3.Click += new EventHandler(lb3_Click);
public void lb3_Click(object sender, EventArgs e)
{
  Label2.Text = "Hello World";
}

Open in new window

0
AshokCommented:
<asp:Panel ID="jobType2Panel" runat="server">
  <asp:Button id="lb2" text="in aspx page" runat="server" OnClick="LinkButton2_Click" />
</asp:Panel>

<asp:Label id="Label2" runat="server" />

code behind:

            LinkButton lb3 = new LinkButton();
            lb3.OnClientClick = "LinkButton2_Click";
            lb3.Text = "in code behind";
            jobType2Panel.Controls.Add(lb3);

HTH
Ashok
0
AshokCommented:
tgerbert is correct.

If you want to add event at run time use his code.

Thanks,
Ashok
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

rajapandian_81Commented:
Try below code to call onclick event of LinkButton2
LinkButton lb3 = new LinkButton();
        lb3.Click += new EventHandler(LinkButton2_Click);
        lb3.Text = "in code behind";
        jobType2Panel.Controls.Add(lb3);

Open in new window

0
AshokCommented:
<asp:Panel ID="jobType2Panel" runat="server">
  <asp:LinkButton id="btn2" text="in aspx page" runat="server"/>
</asp:Panel>
               
<asp:Label id="Label2" runat="server" />

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebAppTest1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btn2.Click += new EventHandler(btn2_Click);
            LinkButton btn3 = new LinkButton();
            btn3.Click += new EventHandler(btn3_Click);

            btn3.Text = "in code behind";
            jobType2Panel.Controls.Add(btn3);
        }

        void btn3_Click(object sender, EventArgs e)
        {
            Label2.Text = "You clicked button using code behind click";  
        }

        void btn2_Click(object sender, EventArgs e)
        {
            Label2.Text = "You clicked the button using ASP click";
        }


    }
}
0
farmingtonisAuthor Commented:
Thanks - This code works now but only on the second click.  Do I need to define it in an init?
0
Todd GerbertIT ConsultantCommented:
You need to create the control, assign a function to it's Click event, and add the control to the panel on every Page_Load.
 
(This example has the code-behind in the .aspx file instead of a separate .aspx.cs file, but the same idea applies)

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	protected void Page_Load(object sender, EventArgs e)
	{
		LinkButton lb3 = new LinkButton();
		lb3.Text = "In Code Behind";
		lb3.Click += new EventHandler(lb3_Click);
		jobType2Panel.Controls.Add(lb3);
	}

	void lb3_Click(object sender, EventArgs e)
	{
		lb2.Text = "You clicked the button.";
	}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="jobType2Panel" runat="server">
                <asp:LinkButton id="lb2" text="in aspx page" runat="server"></asp:LinkButton> 
	</asp:Panel>

		<asp:Label id="Label2" runat="server" />
    </div>
    </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokCommented:
This code works now but only on the second click.

Code I posted is tested and working.

btn2 is created at design time and OnClick is added at run time.
btn3 is created at run time  and OnClick is added at run time.

if you click on any of the LinkButton, it changes TEXT of the LABEL.

HTH
Ashok
0
farmingtonisAuthor Commented:
Thanks I can stop pulling my hair out. barb
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.