0
votes

I have a UserControl call itemUserControl inside an Update Panel.

<asp:UpdatePanel ID="updPanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
         <STX:ItemControl ID="itemUserControl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

Inside the UserControl I have a link button with an OnClick event which I hope to capture from within the user control's code behind.

<asp:LinkButton ID="lnkBtn" runat="server" Text="ClickMe" OnClick="lnkBtn_Click" />

I can't seem to get it working :( From Googling around it looks like I need to add the Triggers to wire up the button click event.

<Triggers>
  <asp:PostBackTrigger ControlID="lnkBtn" EventName="Click" />
</Triggers>

However, I can't add this Trigger on the parent page right after the ContentTemplate tag because Visual Studio doesn't see the button ID. Is there a way to do this? I just want the button event to fire on the user control's code behind.

2

2 Answers

0
votes

You don't need to capture anything unless you want the button to perform a full PostBack. If you just add the lnkBtn_Click method in the code behind of the User Control it will be handled Async.

If you do want a full PostBack, register the button to that event in the Page_load of the UserControl.

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.GetCurrent(Page).RegisterPostBackControl(lnkBtn);
}
0
votes

Remove <Triggers> from your containing page and add following lines in Page_Load event of containing page

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(MyControl.FindControl("lnkBtn"));

This will register your usercontrol and click event of link button will be fired on click