c# - 在 UserControl (ASP.NET) 中时 URL 未被解析

标签 c# asp.net user-controls resolve

如果我向派生类 testA 传递一个包含 HyperlinkPlaceHolder,其 url 开头为 波浪号,它正确地解决了它。 但是,当我通过 testB (除了它继承自 System.Web.UI.UserControl)同PlaceHolder吧 从字面上呈现它(不 转换/解析 '~')

有什么想法吗?

public class testA : System.Web.UI.Control
{
    public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
    protected override void OnLoad(EventArgs e)
    {
        if (plc != null)
            this.Controls.Add(plc);
        base.OnLoad(e);
    }
}


public class testB : System.Web.UI.UserControl
{
    public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
    protected override void OnLoad(EventArgs e)
    {
        if (plc != null)
            this.Controls.Add(plc);
        base.OnLoad(e);
    }
}

这是 ASP.NET

最佳答案

当您从 System.Web.UI.UserControl 继承并且不将您的控件与 ascx 文件相关联时,您的控件 TemplateSourceVirtualDirectory 将未设置,这是 ResolveClientUrl 方法所必需的 - 如果它为 null 或为空,则 url 将按原样返回。

要解决您的问题,只需设置 AppRelativeTemplateSourceDirectory :

public class testB : System.Web.UI.UserControl
{
    public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
    protected override void OnLoad(EventArgs e)
    {
        if (plc != null)
        {
            this.AppRelativeTemplateSourceDirectory =
                    plc.AppRelativeTemplateSourceDirectory;
            this.Controls.Add(plc);
        }
        base.OnLoad(e);
    }
}

关于c# - 在 UserControl (ASP.NET) 中时 URL 未被解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3969546/

相关文章:

c# - Asp.Net MVC 默认路由

c# - 获取处理器 ID 或某些硬件信息的最安全方法

asp.net - 使用 SecretManager 创建 .NET secret

wpf - 异常 : "The assembly used when compiling might be different than that used when loading and the type is missing."

c# - 阻止 VS 将属性值放入 .Designer.cs 文件

html - Viewport 用户可针对笔记本电脑/台式机进行扩展

c# - 如何检查列表的每次迭代中的值

c# - 列出所有系统颜色

c# - 从 Web 应用程序打印到客户端打印机

c# - 如何处理 parent.frames 问题?