为什么动态创建的用户控件消失的时候控制没有做满回发? [英] Why dynamically created user controls disappear when controls are not doing full postbacks?
问题描述
根据我目前的理解,当你有一个UpdatePanel控件没有完全回发。因此,如果动态地添加自定义的用户控件和他们有在他们的$ C $更新ç他们不应该从他们被加载到页面中消失的UpdatePanel,对不对?显然不是。我做了一个简单的项目来测试和仍然点击时,即使他们不应该被触发完全回发我的动态控件消失。我有一个加载控件的aspx页面:
<%@页面语言=C#AutoEventWireup =真codeBehind =TESTmultipleScriptManagerControls.aspx.cs继承=myPlayground.TESTmultipleScriptManagerControls%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV> < ASP:的ScriptManager ID =ScriptManager1=服务器>
< / ASP:ScriptManager的> < ASP:的UpdatePanel ID =UpdatePanel1=服务器>
<&的ContentTemplate GT;
< ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/> < ASP:占位符ID =PLACEHOLDER1=服务器>< / ASP:占位符>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>< / DIV>
< /表及GT;
< /身体GT;
< / HTML>
通过以下code背后:
保护无效的button1_Click(对象发件人,EventArgs的发送)
{ TESTcontrol1 TEMP = LoadControl(TESTcontrol1.ascx)作为TESTcontrol1;
PlaceHolder1.Controls.Add(临时);
TESTcontrol1 TEMP2 = LoadControl(TESTcontrol1.ascx)作为TESTcontrol1;
PlaceHolder1.Controls.Add(TEMP2); }
和一个简单的用户控件:
<%@控制语言=C#AutoEventWireup =真codeBehind =TESTcontrol1.ascx.cs继承=myPlayground.TESTcontrol1%GT;
< ASP:的ScriptManagerProxy ID =ScriptManagerProxy1=服务器>
< / ASP:&的ScriptManagerProxy GT;
< ASP:的UpdatePanel ID =UpdatePanel1=服务器><&的ContentTemplate GT;
< ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/>
< ASP:标签ID =Label1的=服务器文本=标签>< / ASP:标签>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
通过以下code背后:
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
Label1.Text = System.DateTime.Now.ToString();
}
这是为什么管制正在逐渐消失,即使有不应该是一个回传触发任何想法?
OK根据我目前的理解,当你有一个UpdatePanel
控制没有完全回发。
块引用>从回传引发的UpdatePanel始终执行完整的页面生命周期。所有这些事件通常触发。这都没有区别是否使用一个UpdatePanel与否。控件添加每次编程需要重新添加它在每一个回发。
阅读此篇,它可以帮助你了解一个好一点怎么在这里上。
Based on my current understandings, when you have an UpdatePanel control there is no full postback. Therefore if I dynamically add custom user controls and they have UpdatePanels that are updated in their code they shouldnt disappear from the page they are loaded into, right? Apparently not. I made a simple project to test and still my dynamic controls disappear when clicked even though they should not be triggering a full postback. I have an aspx page that loads the controls:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TESTmultipleScriptManagerControls.aspx.cs" Inherits="myPlayground.TESTmultipleScriptManagerControls" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e) { TESTcontrol1 temp = LoadControl("TESTcontrol1.ascx") as TESTcontrol1; PlaceHolder1.Controls.Add(temp); TESTcontrol1 temp2 = LoadControl("TESTcontrol1.ascx") as TESTcontrol1; PlaceHolder1.Controls.Add(temp2); }
And a simple user control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TESTcontrol1.ascx.cs" Inherits="myPlayground.TESTcontrol1" %> <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> </asp:ScriptManagerProxy> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel>
With the following code behind:
protected void Button1_Click(object sender, EventArgs e) { Label1.Text = System.DateTime.Now.ToString(); }
Any ideas on why the controls are disappearing even though there shouldnt be a postback triggering?
解决方案OK based on my current understandings, when you have an UpdatePanel control there is no full postback.
Postbacks triggered from UpdatePanels always execute the full page life-cycle. All the events are triggered normally. It makes no difference whether you use an UpdatePanel or not. Every time you add a control programmatically you need to re-add it on every postback.
Read this post, it may help you understand a bit better what's going on here.
这篇关于为什么动态创建的用户控件消失的时候控制没有做满回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!