[英]Loading a user control dynamically
I'm trying to load a user control in a web service, but it doesn't execute the Load_Page(). 我正在尝试在Web服务中加载用户控件,但是它不执行Load_Page()。
I tried to use: 我尝试使用:
HttpContext.Current.Server.Execute(page, text, false);
But it returns a NullException. 但是它返回一个NullException。 It is my code:
这是我的代码:
public string Controle(string url)
{
Page page = new Page();
UserControl userControl = (UserControl)page.LoadControl(url);
//userControl.EnableViewState = false;
HtmlForm form = new HtmlForm();
form.Controls.Add(userControl);
page.Controls.Add(form);
StringWriter text = new StringWriter();
HtmlTextWriter htmltext = new HtmlTextWriter(text);
userControl.RenderControl(htmltext);
return text.ToString();
}
Hope it is clear. 希望清楚。
You need to initialize the control using a method that does something like 您需要使用类似以下方法的方法来初始化控件
this.Load += Page_Load;
this.Load + = Page_Load;
Please refer to this post which explains exactly what needs to be done to achieve this. 请参阅这篇文章,其中确切说明了实现此目的需要执行的操作。 Be sure to read the section stating;
请务必阅读本节中的说明; " One word of caution: " http://weblogs.asp.net/srkirkland/archive/2007/11/05/dynamically-render-a-web-user-control.aspx This related post will also be able to assist: Page_Load not firing in UserControl
“ 一个警告的提示: ” http://weblogs.asp.net/srkirkland/archive/2007/11/05/dynamically-render-a-web-user-control.aspx此相关文章也将能够为您提供帮助: 无法在UserControl中触发Page_Load
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.