10

I have a user control placed inside an update panel, ie like this.

<asp:UpdatePanel ID="testupdatepnl" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
                        <uc1:TestControl ID="ctlTest" runat="server" />
        </ContentTemplate>
</asp:UpdatePanel>

Now i got a button placed inside this user control say Click. I want to postback the whole page on the button click. I tried to add a postback trigger like this

<Triggers>
    <asp:PostBackTrigger ControlID="clickButton" />
</Triggers>

Since the button is inside the usercontrol , i got error while running like this.

Is there any way to do a postback for this button.

Mahesh KP
  • 6,248
  • 13
  • 50
  • 71
  • 1
    http://stackoverflow.com/questions/5558049/trigger-a-button-inside-a-updatepanel-with-a-loginview – Nalaka526 May 14 '12 at 04:25
  • @Nalaka526 : but i got an error like 'A control with ID 'ctl00$phContent$ctlList$ctlClientLocations$clickButton' could not be found for the trigger in UpdatePanel 'testupdatepnl'." – Mahesh KP May 14 '12 at 04:49
  • Can you please send the code you have modified i.e. assigning control's UniqueID serverside as trigger – Imran Rizvi May 14 '12 at 07:10

3 Answers3

16

Remove the <Triggers> from HTML & Add this to PageLoad event

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(WebUserControl11.FindControl("ButtonId")); 

Note : Learned from this

Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
Nalaka526
  • 11,278
  • 21
  • 82
  • 116
  • But i have another button in the page and want to do a postback on its click, for that button i have added the trigger in the page. – Mahesh KP May 14 '12 at 06:41
  • @mahesh So then keep the trigger in html with the postback trigger for the Page Button, and add add postback trigger for the UserControl Button on PageLoad. It should work... – Nalaka526 May 14 '12 at 06:55
  • fyi, this worked for me by putting this code in the page_load of the usercontrol itself, which keeps you from having to put usercontrol-specific code in the consumer of a usercontrol. Thank you so much for the tip though! – Andy Arndt Mar 09 '19 at 01:03
0

Because button you are using as trigger is placed inside update panel and after render its client id gets changed.

Make the button accessable from user control and declare the trigger at server side e.g.

var ClickButton= ctlTest.FindControl("clickButton") as Button;


var trigger = new PostBackTrigger();
trigger.ControlID = ClickButton.UniqueID.ToString();
testupdatepnl.Triggers.Add(trigger);

See the following thread for more details Trigger a button inside a UpdatePanel with a LoginView

Community
  • 1
  • 1
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
0

You should also note that if you are using Ajax Control Toolkit. You can replace ScriptManager to ToolkitScriptManager.

Dim btn As Button = CType(UserControl.FindControl("ButtonID"), Button)        
AjaxControlToolkit.ToolkitScriptManager.GetCurrent(Page).RegisterPostBackControl(btn)
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
nelson
  • 1