为什么当控件没有进行完整的回发时动态创建的用户控件会消失? [英] Why dynamically created user controls disappear when controls are not doing full postbacks?

查看:19
本文介绍了为什么当控件没有进行完整的回发时动态创建的用户控件会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我目前的理解,当您拥有 UpdatePanel 控件时,没有完整的回发.因此,如果我动态添加自定义用户控件并且它们的代码中更新了 UpdatePanel,它们不应该从它们加载到的页面中消失,对吗?显然不是.我做了一个简单的项目来测试,但我的动态控件在单击时仍然消失,即使它们不应该触发完整的回发.我有一个加载控件的 aspx 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TESTmultipleScriptManagerControls.aspx.cs" Inherits="myPlayground.TESTmultipleScriptManagerControls" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 过渡//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><标题></标题></头><身体><form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><asp:UpdatePanel ID="UpdatePanel1" runat="server"><内容模板><asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/><asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder></内容模板></asp:UpdatePanel></div></表格></身体></html>

后面有以下代码:

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);}

还有一个简单的用户控件:

<%@ 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"><内容模板><asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></内容模板></asp:UpdatePanel>

后面有以下代码:

protected void Button1_Click(object sender, EventArgs e){Label1.Text = System.DateTime.Now.ToString();}

关于为什么控件消失的任何想法,即使不应该触发回发?

解决方案

根据我目前的理解,当你有一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆