I have a usercontrol that move dynamically on the CreateChildControls method some web controls. When using this control into a page like :
我有一个usercontrol,可以在CreateChildControls方法上动态移动一些Web控件。将此控件用于以下页面时:
<myControls:MyUserControl runat="server" ID="myUserControl" />
It works flawlessly.
它完美无瑕。
But if I want to add that userControl dynamically into a page like :
但是,如果我想将userControl动态添加到页面中,如:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="plhControls" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnAdd" OnClick="btnAdd_Click" />
.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
MyUserControl myUserControl =(MyUserControl)LoadControl("~/Controls/MyUserControl.ascx");
myUserControl.ID = "test";
plhControls.Controls.Add(myUserControl );
}
It crashes inside my CreateChildControls with error : The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
它在我的CreateChildControls中崩溃并出现错误:在DataBind,Init,Load,PreRender或Unload阶段期间无法修改控件集合。
The exact line is when I add the webControls to the placeholder inside my UserControl :
确切的行是当我将webControls添加到UserControl中的占位符时:
plhContent.Controls.Add(myWebControl);
So I don't understand why in this case I cannot move a web control inside the CreateChildControls event when I add my UserControl dynamically.
所以我不明白为什么在这种情况下,当我动态添加UserControl时,我无法在CreateChildControls事件中移动Web控件。
2 个解决方案
#1
Since LoadControl catch up every event of the loaded control to the current event, it is impossible to run the CreateChildControls at the same time as the parent since the event already occurred ed at that time.
由于LoadControl将加载控件的每个事件都捕获到当前事件,因此不可能在父事件同时运行CreateChildControls,因为此时事件已经发生。
In my case I cannot add dynamically my UserControl, so I did the opposite to reproduce the same behaviour I wanted. I initialize all my controls, and remove those I don't want OnClick. This add unneeded load time, but it is acceptable in the scenario I use it.
在我的情况下,我不能动态添加我的UserControl,所以我做了相反的重现我想要的相同行为。我初始化我的所有控件,并删除那些我不想要的OnClick。这会增加不必要的加载时间,但在我使用它的场景中是可以接受的。
#2
The controls have to be initialised and brought up to speed with the rest of the controls as far as the "page lifecycle" is concerned. Have a look at the Page.LoadControl method, and see if it's possible to create your control earlier on.
就“页面生命周期”而言,必须初始化控件并使其与其他控件保持同步。看看Page.LoadControl方法,看看是否可以在之前创建控件。
The method we sometimes use involves using a postback parameter from the page. Don't forget that you will need to re-add your control to the form fot every postback, as it will not get created for you
我们有时使用的方法涉及使用页面中的回发参数。不要忘记,您需要将控件重新添加到每个回发的表单中,因为它不会为您创建
#1
Since LoadControl catch up every event of the loaded control to the current event, it is impossible to run the CreateChildControls at the same time as the parent since the event already occurred ed at that time.
由于LoadControl将加载控件的每个事件都捕获到当前事件,因此不可能在父事件同时运行CreateChildControls,因为此时事件已经发生。
In my case I cannot add dynamically my UserControl, so I did the opposite to reproduce the same behaviour I wanted. I initialize all my controls, and remove those I don't want OnClick. This add unneeded load time, but it is acceptable in the scenario I use it.
在我的情况下,我不能动态添加我的UserControl,所以我做了相反的重现我想要的相同行为。我初始化我的所有控件,并删除那些我不想要的OnClick。这会增加不必要的加载时间,但在我使用它的场景中是可以接受的。
#2
The controls have to be initialised and brought up to speed with the rest of the controls as far as the "page lifecycle" is concerned. Have a look at the Page.LoadControl method, and see if it's possible to create your control earlier on.
就“页面生命周期”而言,必须初始化控件并使其与其他控件保持同步。看看Page.LoadControl方法,看看是否可以在之前创建控件。
The method we sometimes use involves using a postback parameter from the page. Don't forget that you will need to re-add your control to the form fot every postback, as it will not get created for you
我们有时使用的方法涉及使用页面中的回发参数。不要忘记,您需要将控件重新添加到每个回发的表单中,因为它不会为您创建