Link to home
Start Free TrialLog in
Avatar of farmingtonis
farmingtonis

asked on

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";
    }
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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

<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
tgerbert is correct.

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

Thanks,
Ashok
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

<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";
        }


    }
}
Avatar of farmingtonis
farmingtonis

ASKER

Thanks - This code works now but only on the second click.  Do I need to define it in an init?
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks I can stop pulling my hair out. barb