开发者

URL's not being resolved when in UserControl (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(Event开发者_运维百科Args e)
    {
        if (plc != null)
            this.Controls.Add(plc);
        base.OnLoad(e);
    }
}

This is ASP.NET


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜