在 UserControl (ASP.NET) 中时 URL 未得到解析

发布于 2024-09-27 18:54:11 字数 830 浏览 2 评论 0原文

如果我向派生类 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

If I pass the derived class testA a PlaceHolder that contains a Hyperlink, with a url that starts with
a tilde, it resolves it correctly.
However, when I pass testB
(identical apart from it inherits from
System.Web.UI.UserControl) the same PlaceHolder It
renders it literally (doesn't
transform / resolve the '~')

Any ideas?

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);
    }
}

This is ASP.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-10-04 18:54:11

当您继承 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);
    }
}

When you inherit from System.Web.UI.UserControl and do not associate your control with an ascx file then your control TemplateSourceVirtualDirectory will not be set, this is required by the ResolveClientUrl method - if its null or empty the url will be returned AS IS.

To solve your problem, just set 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);
    }
}
_畞蕅 2024-10-04 18:54:11

UserControl 通常与 ascx 文件定义其标记。此类控件应使用 TemplateControl.LoadControl() 实例化 在将它们添加到页面之前,以便执行事件追赶。

我怀疑事件追赶不会发生,因为您没有调用 LoadControl(),因此 HyperlinkNavigateUrl 永远不会获取有机会得到妥善解决。

A UserControl is normally associated with an ascx file that defines its markup. Such controls should be instantiated using TemplateControl.LoadControl() before they're added to the page, in order to perform event catch-up.

I suspect that event catch-up does not take place since you don't call LoadControl(), so the Hyperlink's NavigateUrl never gets a chance to be properly resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文