[英]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.