[英]Populate controls of dynamically instantiated Page from ASPX file?
當前,我只有一個Example.aspx文件( 后面沒有代碼 ),我想加載它,填充它具有的控件,獲取它的輸出並使用它(在http處理程序內)進行某些操作。
我在做什么是這樣的:
// Gets the page and instantiates it?
Type type = BuildManager.GetCompiledType("~/Example.aspx");
Page page = (Page)Activator.CreateInstance(type);
// ProcessRequest of page here?
// Error happens here, the page doesn't have any controls (but there is a label).
((Label)page.FindControl("Label")).Text = "Hello World";
using (StringWriter output = new StringWriter())
{
// Execute the page and output the result into the string writer.
HttpContext.Current.Server.Execute(page, output, false);
// Do something with the output (or save it, email it, etc)
// ...in this case we render it.
context.Response.ContentType = "text/html";
context.Response.Write(output.ToString());
}
但這不起作用,因為頁面實例沒有任何控件(需要創建子控件嗎?)。
如果我添加:
page.ProcessRequest(HttpContext.Current);
它可以工作,但是我認為它可以運行整個頁面生命周期,其中包括將頁面呈現給響應,這是我不想要的。
當使用激活器創建頁面實例時,可以與init或load事件掛鈎,以在處理http請求時執行其他代碼。 不要忘記它仍然是事件驅動的模型! 希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.