简体   繁体   English

单击此用户控件内的按钮后,动态更新更新面板内的用户控件消失

[英]Dynamically user control inside update panel disappear after click button inside this user control

When click the button inside the user control it seems its not do its event (button click) I just want reload the update panel and this user control take the new effect in this button click event 当单击用户控件内的按钮时,似乎未执行其事件(按钮单击),我只想重新加载更新面板,并且此用户控件在此按钮单击事件中产生新的效果

I add user control on html control inside the update panel like this 我像这样在更新面板中的html控件上添加用户控件

<!-- language: c# -->
content.Controls.Remove(content.FindControl("currentusercontrol"));
    UserControl usr1 = (UserControl)LoadControl("myusercontrol.ascx");
    usr1.ID = "currentusercontrol";
   content.Controls.Add(usr1);

And this my parent page content 这是我的父页面内容

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True"> </asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" OnLoad="UpdatePanel1_Load" OnPreRender="UpdatePanel1_PreRender">

        <ContentTemplate>

    <div id="content" class="content" runat="server">
        <!-- begin breadcrumb -->

        <!-- end breadcrumb -->
        <!-- begin page-header -->
        <h1 class="page-header" runat="server" id="pageheader">Dashboard v2 <small id="smallheader" runat="server">header small text goes here...</small></h1>
        <!-- end page-header -->

    </div>
            </ContentTemplate>
        <Triggers>           
        </Triggers>           
    </asp:UpdatePanel>

and this is my user control page 这是我的用户控制页面

<%@ Control Language="C#" ClassName="Contact_Us" %>
<%@ Import Namespace="System.Threading" %>
<script runat="server">
private void Button1_OnClick(object sender, EventArgs e)
{
    Thread.Sleep(5000);
    Label1.Text = "lion";
}
</script>

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" ></asp:ScriptManagerProxy>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Submit" OnClick="Button1_OnClick"  />

i try this but its not work 我尝试这个但它不起作用

<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />

I tried to put trigger directly to update panel but I had error there is no control with this id because button is in user control so I tried to put the trigger programmatically on user control load event Like this 我试图将触发器直接放置到更新面板上,但是我有一个错误,没有此ID的控件,因为按钮位于用户控件中,所以我尝试以编程方式将触发器置于用户控件加载事件上,例如

    UpdatePanel UpdatePanel1 = this.Parent.FindControl("UpdatePanel1") as UpdatePanel;
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
    trigger.ControlID = Button1.ID;
    trigger.EventName = "Click";
    UpdatePanel1.Triggers.Add(trigger);

But I have same error control with ID 'Button1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'. 但是我在UpdatePanel'UpdatePanel1'中找不到与ID为'Button1'相同的错误控件。

then itried to use client id for button1 like this 然后试图像这样为button1使用客户端ID

trigger.ControlID = Button1.ClientID;

same problem A control with ID 'currentusercontrol_Button1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'. 同样的问题在UpdatePanel'UpdatePanel1'中找不到ID为'currentusercontrol_Button1'的控件作为触发器。

Modify what you want inside Update Panel (via asp tags ID's) at button click and call UpdatePanel1.Update(); 单击按钮,在更新面板中修改所需内容(通过asp标签ID),然后调用UpdatePanel1.Update();。

http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.update(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.updatepanel.update(v=vs.110).aspx

i found it i just put this code inside updatepanel load and all thing went fine 我发现它只是将这段代码放入updatepanel加载中,一切正常

        content.Controls.Remove(content.FindControl("currentusercontrol"));
        UserControl usr1 = (UserControl)LoadControl("myusercontrol");
        usr1.ID = "currentusercontrol";
        content.Controls.Add(usr1);

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

相关问题 未定义更新面板内部动态添加的用户控件内部的Javascript函数 - Javascript Function Inside Dynamically Added User Control Inside Update Panel Is Not Defined 在更新面板内的转发器内的用户控件内创建的JQuery函数不起作用 - JQuery function created inside a user control inside a repeater inside an update panel is not working 动态创建用户控件中的jQuery - jquery inside User Control created dynamically 用户控件内的RegisterStartupScript - RegisterStartupScript inside user control Javascript代码停止在带有Update Panel的ASP.NET用户控件中工作 - Javascript code stopped working inside a ASP.NET user control with Update Panel 弹出窗口中放置的Web用户控件的按钮单击事件未触发-Asp.net Webforms - Button Click Event of Web User Control that is placed inside a popup not firing - Asp.net Webforms 在用户控件中使用getElementById - Using getElementById inside user control 可以在按钮的click事件上呈现用户控件吗? - Can a user control be rendered on the click event of a button? 如何在用户控件中获取javascript以在页面加载可见性为false的面板中工作? - How go I get the javascript in my user control to work inside of a panel whose visibility is false on page load? jQuery代码未在用户控件ASPX内部触发 - Jquery code not firing inside user control aspx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM