UserControl click event won't fire

M

Mark Sisson

I've got a custom usercontrol (UC1) that dynamically loads another
custom usercontrols (UC2) during it's Init event. Both controls have
LinkButtons with click events. The LinkButton from UC1 properly goes
to it's click-event handler but the LinkButton from UC2 does not fire
it's associated click event.


Are there problem in having nested Usercontrols fire events if they're
created through code?

TIA
 
T

Thijs

I think you're experiencing the same problem as I am. The problem is that
ASP.NET is unable to restore the viewstate of a dynamically loaded control
after a postback. I'm searching for a way to do this manually now.

Regards,

Thijs
 
C

Chris Thayer

This should work correctly. I created an example like you described and it
worked as expected. Here is the source of UC1:

<%@ control language="C#" classname="WebUserControl"%>
<%@ register tagprefix="uc" tagname="uc2" src="~/WebUserControl2.ascx" %>

<script runat="server">
protected override void OnInit(EventArgs e)
{
WebUserControl2 uc2 =
(WebUserControl2)LoadControl("WebUserControl2.ascx");
Controls.Add(uc2);
base.OnInit(e);
}

void LinkButton1_Click(object sender, EventArgs e)
{
Page.Response.Write("this is from LinkButton1_Click<br>");
}

</script>

<asp:linkbutton runat="server" id="LinkButton1" onclick="LinkButton1_Click"
LinkButton1</asp:linkbutton><br />

and this is the source of UC2:

<%@ control language="C#" classname="WebUserControl2"%>

<script runat="server">

void LinkButton2_Click(object sender, EventArgs e)
{
Page.Response.Write("this is from LinkButton2_Click<br>");
}
</script>

<asp:linkbutton runat="server" id="LinkButton2" onclick="LinkButton2_Click"
LinkButton2</asp:linkbutton>

Hope that helps,
Chris Thayer
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top