سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top