Postback trigger for button inside a usercontrol in Updatepanel

倾然丶 夕夏残阳落幕 提交于 2019-12-08 17:30:24

问题


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.


回答1:


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




回答2:


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




回答3:


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)


来源:https://stackoverflow.com/questions/10577474/postback-trigger-for-button-inside-a-usercontrol-in-updatepanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!