Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(EventArgs e)
    {
        if (plc != null)
            this.Controls.Add(plc);
        base.OnLoad(e);
    }
}

This is ASP.NET

like image 203
maxp Avatar asked Oct 19 '10 14:10

maxp


1 Answers

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);
    }
}
like image 165
MK. Avatar answered Oct 06 '22 01:10

MK.