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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10577474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:22:02  来源:igfitidea点击:

Postback trigger for button inside a usercontrol in Updatepanel

c#asp.netupdatepanelpostbackajaxcontroltoolkit

提问by Mahesh KP

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

现在我在这个用户控件中放置了一个按钮,比如 Click。我想在单击按钮时回发整个页面。我尝试添加这样的回发触发器

<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.

有什么办法可以为这个按钮做一个回发。

回答by Imran Rizvi

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 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

有关更多详细信息,请参阅以下线程 使用 LoginView 触发 UpdatePanel 内的按钮

回答by Nalaka526

Remove the <Triggers>from HTML & Add this to PageLoadevent

<Triggers>从 HTML 中删除& 将此添加到PageLoad事件

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

Note :Learned from this

注意:这里学到

回答by nelson

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

您还应该注意,如果您使用的是 Ajax Control Toolkit。您可以将 ScriptManager 替换为 ToolkitScriptManager。

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