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:Lin kButton>
</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";
}
<asp:Panel ID="jobType2Panel" runat="server">
<asp:LinkButton id="lb2" text="in aspx page" runat="server" OnClick="LinkButton2_Click
</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
}
public void LinkButton2_Click(Object sender, EventArgs e)
{
Label2.Text = "You clicked the link button";
}
<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
<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
HTH
Ashok
tgerbert is correct.
If you want to add event at run time use his code.
Thanks,
Ashok
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);
<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";
}
}
}
<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
}
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";
}
}
}
ASKER
Thanks - This code works now but only on the second click. Do I need to define it in an init?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Thanks I can stop pulling my hair out. barb
Open in new window