简体   繁体   English

Updatepanel中用户控件内的按钮的回发触发器

[英]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. 现在我在这个用户控件里面放了一个按钮说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. 有没有办法为这个按钮做回发。

Remove the <Triggers> from HTML & Add this to PageLoad event 从HTML中删除<Triggers>并将其添加到PageLoad事件

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

Note : Learned from this 注:从得知这个

Because button you are using as trigger is placed inside update panel and after render its client id gets changed. 因为您用作触发器的按钮位于更新面板内,并且在渲染后其客户端ID被更改。

Make the button accessable from user control and declare the trigger at server side eg 使用户控件可以访问该按钮,并在服务器端声明触发器,例如

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 有关更多详细信息,请参阅以下线程使用LoginView触发UpdatePanel内的按钮

You should also note that if you are using Ajax Control Toolkit. 您还应该注意,如果您使用的是Ajax Control Toolkit。 You can replace ScriptManager to ToolkitScriptManager. 您可以将ScriptManager替换为ToolkitScriptManager。

Dim btn As Button = CType(UserControl.FindControl("ButtonID"), Button)        
AjaxControlToolkit.ToolkitScriptManager.GetCurrent(Page).RegisterPostBackControl(btn)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM