动态加载与LoadControl方法一个用户控件(类型,对象[]) [英] Dynamically Loading a UserControl with LoadControl Method (Type, object[])

查看:130
本文介绍了动态加载与LoadControl方法一个用户控件(类型,对象[])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个页面的方法返回用户/服务器控件的HTML重新presentation。当我打电话这需要用户控件的虚拟路径的过载,而不是当我尝试调用带一个类型的重载它的工作原理。样品code如下。有什么建议?

I'm trying to return the html representation of a user/server control through a page method. It works when I call the overload which takes the virtual path to the user control, but not when I try to call the overload which takes a type. The sample code is below. Any suggestions?

[WebMethod]
public static string LoadAlternates(string productId, string pnlId)
{
    object[] parameters = new object[] { pnlId, productId };
    return ControlAsString(typeof(PopupControl), parameters);
}

private static string ControlAsString(Type controlType, object[] parameters)
{
    Page page = new Page();
    UserControl controlToLoad;
    /*
     *calling load control with the type results in the 
     *stringwriter returning an empty string
    */

    controlToLoad = page.LoadControl(controlType, parameters) as UserControl;
    /*
     *However, calling LoadControl with the following overload
     *gives the correct result, ie the string rep. of the control.
    */
     controlToLoad = page.LoadControl(@"~/RepeaterSamples/PopupControl.ascx") as UserControl;

    //some more code, then this... 
    page.Controls.Add(controlToLoad);

    StringWriter sw = new StringWriter();
    HttpContext.Current.Server.Execute(page, sw, false);
    return sw.ToString();
}

任何想法,为什么这样的StringWriter将返回一个空字符串?我要指出的是,所有的页生命周期执行选择调用LoadControl方法正确,不论

Any ideas why this StringWriter would return an empty string? I should point out that all the "page" lifecycle execute correctly irrespective of the method chosen to call LoadControl.

想添加 -
我的有无的使用 LoadControl(类型,对象[])超载。 : - (

Wanted to add - I have to use the LoadControl(Type, object[]) overload. :-(

推荐答案

为LoadControl的MSDN页面有在底部此评论:

描述
,使用的加载用户控件的页
  Page.LoadControl(类型,对象[])似乎并不创建其子
  在ASCX文件中添加的。使用Page.LoadControl(字符串)的工作原理
  预计。

Description
A page that loads a user control using the Page.LoadControl(Type, Object[]) does not seem to create its children added in the ascx file. Using Page.LoadControl(String) works as expected.


感谢您提出这个问题的评论。我们正在调查,并
  将提供一个更新的状态,当我们有更多的信息。

Comments
Thank you for submitting this issue. We're investigating and will provide an update on status when we have more information.

-The Web平台和放大器;工具团队
发布Microsoft在2005年8月6日在上午11:08
这是设计,因为类型TestUC实际上是
  通过局部使用类基本类型,它不包含正确的
  code实例TextBox1的参考,这是在实际定义
  派生类型。有两种解决方法:1。使用
  LoadControl(TestControl.ascx),对于所有的实际,这个行为
  以相同LoadControl(类型),但它实例化派生类型,
  它知道如何实例TextBox1的。 2.使用单个文件网页,
  增加与LT;%@参考%>指令到页面引用用户
  控制,并分配一个类名的ASCX页。然后,它是安全的
  使用LoadControl(类型)

-The Web Platform & Tools Team
Posted by Microsoft on 8/06/2005 at 11:08 AM
This is by-design since the type "TestUC" is actually the base type used by the partial class, it does not contain the proper code to instantiate TextBox1 reference, which is actually defined in the derived type. There are two workarounds: 1. Use LoadControl("TestControl.ascx"), for all practical, this behaves identically to LoadControl(type) but it instantiates the derived type, which knows how to instantiate TextBox1. 2. Use a single file page and adds <%@ Reference %> directive to the page to reference the user control, and assign a classname to the ascx page. Then it's safe to use LoadControl(type)

感谢您报告的问题。
Web平台和工具团队。发布者
  微软在14/06/2005下午6时31分

Thanks for reporting the issue.
Web Platform and Tools Team. Posted by Microsoft on 14/06/2005 at 6:31 PM

这篇关于动态加载与LoadControl方法一个用户控件(类型,对象[])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆