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?

 

 

转载于:https://www.cnblogs.com/chucklu/p/11139547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值