1

I have a page with a UserControl (in which linkbuttons are dynamically created). On the page I have a UpdatePanel. What I need to do is to add a AsynchronPostBackTrigger to the UpdatePanel but pointing from a LinkButton that is inside the UserControl.

(in the .aspx)

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
      <Triggers>
     <asp:AsyncPostBackTrigger ControlID="UserControl`s LinkButton" EventName="Click"
     </Triggers>       
 </asp:UpdatePanel>

So whenever i click the LinkButton inside the UserControl, the UpdatePanel on the page will refresh.

Cœur
  • 37,241
  • 25
  • 195
  • 267
marko_net
  • 43
  • 7
  • Are there any postbacks fired in the user control that should NOT cause an async postback? Meaning can you just put the usercontrol's ID there so that any postbacks inside of it are all triggers? – Servy Mar 29 '12 at 20:02
  • possible duplicate of [Trigger an update of the UpdatePanel by a control that is in different ContentPlaceHolder](http://stackoverflow.com/questions/417131/trigger-an-update-of-the-updatepanel-by-a-control-that-is-in-different-contentpl) – Justin Pihony Mar 29 '12 at 20:04
  • @JustinPihony: That is not a duplicate. – Tim Schmelter Mar 29 '12 at 20:38

1 Answers1

1

In my opinion the best approach would be to provide a custom event in your UserControl which gets raised on LinkButton-Click and can be used as AsyncPostbackTrigger in your page's UpdatePanel.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
     <Triggers>
        <asp:AsyncPostBackTrigger ControlID="UserControl1" EventName="LinkClicked" />
    </Triggers>       
</asp:UpdatePanel>

For example in your UserControl:

public delegate void OnLinkClicked(object sender, EventArgs e);
public event OnLinkClicked LinkClicked;

protected void LinkButton_Clicked(Object sender, EventArgs e)
{
    LinkClicked(sender, e);
}

Then in your page:

protected void Page_Init(object sender, EventArgs e)
{
    MyControl1.LinkClicked += LinkClicked;
}

private void LinkClicked(object sender, EventArgs e)
{
    //this is an AsyncPostBack;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Tahnks for the response Tim. Im not sure what u mean, how would i use the Click event as a AsyncPostbackTrigger? Let`s say i catch the event in my page, what should i use then? – marko_net Mar 29 '12 at 20:18
  • Upps i didn`t see the rest or the answer, i`ll try this. – marko_net Mar 29 '12 at 20:23
  • @marko_net: Edited to show what to do in the page. Apart from that the signature of the event must contain `sender` and `EventArgs` for an UpdatePanel trigger. – Tim Schmelter Mar 29 '12 at 20:29
  • Error says: Could not find an event named 'btn_Clicked' on associated control 'UserControl1' for the trigger in UpdatePanel 'UpdatePanel1'. (Page .aspx) – marko_net Mar 29 '12 at 23:33
  • (Page .cs) protected void btn_Clicked(object sender, EventArgs e) { Label1.Text = "BBBB"; UpdatePanel1.Update(); } – marko_net Mar 29 '12 at 23:33
  • @marko_net: Please look carefully at my (tested) sample code. `EventName="LinkClicked"` which is the event for the trigger is the event in the UserControl, not the event handler in the page. – Tim Schmelter Mar 30 '12 at 07:27