简体   繁体   English

如何使用UpdatePanel以编程方式添加到页面的方式创建和使用自定义控件

[英]How to create and use custom control with UpdatePanel added to the page programmatically

I have simple web user control (the code I found somewhere in the web): 我有简单的网络用户控件(我在网络中某处找到的代码):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="ARP.DynamicsCRM2011.MagicWebForm.WebUserControl1" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged"
        Text="Checkbox" />
    <asp:Button ID="Button1" runat="server" Text="Button" Visible="False" OnClick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

protected void Page_Load(object sender, EventArgs e)
{

}

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    Button1.Visible = CheckBox1.Checked;
}

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

Now I want to add this control to my page (programmatically in OnLoadComplete event): 现在,我想将此控件添加到我的页面中(以编程方式在OnLoadComplete事件中):

<%@ Reference Control="~/WebUserControl1.ascx" %>

WebUserControl1 myControl = (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx");
myControl.ID = "myControl_" + some_name;
parentControl.Controls.Add(myControl);

Of course I have SriptManager on the page and my control is added properly. 当然,我的页面上有SriptManager,并且控件已正确添加。 I know that programmatically added controls must be recreated every time the page is loaded. 我知道每次页面加载时都必须重新创建以编程方式添加的控件。 Unfortunately this causes creating new control, so checking checkbox doesn't work - after checking it the OnLoadComplete (of the page) is fired again and new control is created. 不幸的是,这导致创建新控件,因此无法选中复选框-在选中它之后,将再次触发(页面的)OnLoadComplete,并创建新控件。 If I omit that then nothing is displayed. 如果我忽略了,则什么也不显示。 So the question is - how to do this? 所以问题是-如何做到这一点?

Dynamic control should be re-added to the control tree OnPreInit , see documentation: 动态控件应重新添加到控件树OnPreInit ,请参阅文档:

PreInit - Create or re-create dynamic controls. PreInit-创建或重新创建动态控件。

ASP.NET Page Life Cycle Overview ASP.NET页面生命周期概述

You can add that control on button click and save count of added controls in some storage like Session . 您可以在单击按钮时添加该控件,并将添加的控件的数量保存在某些存储中,例如Session The in Page_Init you need to check that count value and recreate each control you have add before. Page_Init您需要检查该计数值并重新创建之前添加的每个控件。

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

相关问题 添加到“自定义控件”时触发UpdatePanel消失 - Trigger for UpdatePanel disappearing when added to a “custom control” 无法以编程方式添加自定义控件 - Custom control not able to be added programmatically 如果在Page_Load中添加了UpdatePanel,则无法在ascx控件中使用 - UpdatePanel not working in ascx control if is added on Page_Load 如何以编程方式将UpdatePanel控件添加到页面? - How to add an UpdatePanel controls to a page programmatically? 如何将以编程方式添加的文字控件放置在网页的正确位置? - How to place a programmatically added literal control in the right location in the web page? 如果控件Button在UpdatePanel外部,如何使用UpdateProgress - How to use UpdateProgress if control Button is outside UpdatePanel 如何在UpdatePanel中添加我自己的自定义用户控件? - How to add my own custom User Control inside an UpdatePanel? 是否可以通过编程方式添加到面板的自定义控件中访问和更改值? - Is there a way to access and change a value in a custom control that was added, programmatically, to a panel? 如何以编程方式创建LongListSelector控件 - How to create a LongListSelector control programmatically 以编程方式添加的用户控件不会创建其子控件 - Programmatically added User Control does not create its child controls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM