views:

204

answers:

2

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

+1  A: 

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.

Frédéric Hamidi
I was curious as to whether `testB` would work even when instantiated using `LoadControl(Type, object[])` and it doesn't. Which is not too surprising, since as you point out, UserControls are meant to have associated .ascx and either be declared in the markup or loaded using LoadControl.
Chris F