The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

https://stackoverflow.com/questions/5508666/dynamically-add-html-to-asp-net-page

 

 

https://stackoverflow.com/questions/4975823/adding-to-page-control-collection-from-inside-a-user-control

在page上添加多个相同的file upload control(自定义的控件),现在想要在添加自定义控件的同时,注入一段HTML

 

 

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 

 
 

Adding to page control collection from inside a user control

Usercontrol doesn't have a PreInit event, but nothing's stopping you from adding a handler to the Page PreInit event in your UserControl:

Page.PreInit += new EventHandler(Page_PreInit);

edit - you're right - you can't capture PreInit from a usercontrol, though I'm surprised you can't - but you can still change the page controls collection by adding code to the constructor of the UserControl. I tried this and it works.

public MyUsercontrol()
{
    Page page = (Page)HttpContext.Current.Handler;
    Literal lit = new Literal();
    lit.Text="text";
    page.Controls.Add(lit);
}

Adding an event handler to Page.PreInit in the constructor compiles but it never fires.

(end edit)

That said, I'm not exactly sure why this is necessary to achieve your goal. Why don't you just have your dialog control render it's own div in-line wherever you drop it into the form, and use that as the parent, instead of trying to create one somewhere else in the form? I can't think of why it would be important for it to physically be rendered at the beginning or end of the form. It's a dialog so it will always be invisible until you use it, right?

 

 

posted @ 2019-07-05 17:14  ChuckLu  阅读(302)  评论(0编辑  收藏  举报