如何创建和使用自定义的控制与UpdatePanel中添加到页面编程 [英] How to create and use custom control with UpdatePanel added to the page programmatically
问题描述
我有简单的Web用户控件(code我在网上找到的地方):
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();
}
现在我要将此控件添加到我的网页(以编程方式的onLoadComplete事件):
Now I want to add this control to my page (programmatically in OnLoadComplete event):
<%@ Reference Control="~/WebUserControl1.ascx" %>
WebUserControl1 myControl = (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx");
myControl.ID = "myControl_" + some_name;
parentControl.Controls.Add(myControl);
当然,我有SriptManager在页面上,我控制的正确添加。我知道以编程方式添加控件必须每次页面加载时重新创建。不幸的是这将导致创建新的控制,因此检查复选框不工作 - 检查它的onLoadComplete的(页面)再次发射,并创建新的控制之后。如果我省略则不显示任何内容。所以,问题是 - 如何做到这一点。
Of course I have SriptManager on the page and my control is added properly. 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. If I omit that then nothing is displayed. So the question is - how to do this?
推荐答案
动态控制应重新添加到控制树在preINIT
,看文档:
Dynamic control should be re-added to the control tree OnPreInit
, see documentation:
preINIT - 创建或重新创建动态控件
PreInit - Create or re-create dynamic controls.
这篇关于如何创建和使用自定义的控制与UpdatePanel中添加到页面编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!