Mahesh KP
Mahesh KP

Reputation: 6446

Postback trigger for button inside a usercontrol in Updatepanel

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.

Upvotes: 10

Views: 27038

Answers (3)

nelson
nelson

Reputation: 1

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)

Upvotes: 0

Nalaka526
Nalaka526

Reputation: 11464

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

Upvotes: 16

Imran Rizvi
Imran Rizvi

Reputation: 7438

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

Upvotes: 0

Related Questions