Thomas Bandt

Über mich | Kontakt | Archiv

LoadControl() - Bug in ASP.NET 2.0!?

Da bin ich wohl auf einen schwerwiegenden Bug in ASP.NET 2.0 gestoßen. Und zwar ist es unmöglich via LodControl ein Control außerhalb einer CodeBeside-Klasse zu laden. Jeglicher Versuch das in einer eigenen Klasse zu tun, scheitert mit der Fehlermeldung:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: basepath

Die Ursache laut Raomond Brookman: die Eigenschaft AppRelativeTemplateSourceDirectory wird nicht richtig initialisiert. Sein angebotener Workaround funktionierte nur leider in meinen Testfällen nicht.

Nachdem ich damit fast einen Arbeitstag verbraten habe, habe ich jetzt eine Dirty-Method gewählt: ich übergebe an meine Klasse einfach die Page-Instanz des aufrufenden WebForms mit, und arbeite dann mit dieser. So funktioniert es. Das kann man als Property oder auch als normalen Parameter für eine statische Methode machen:

protected void Page_Load(object sender, EventArgs e)
{
   RenderContent content = new RenderContent();
   content.ContentPlaceholder = MeinPlaceholderXYZ;
   content.Instance = this;
   content.Render();
}

Instance ist eine public Property:

private Page instance;

public Page Instance
{
   get { return instance; }
   set { instance = value; }
}

Mit dieser kann man nun arbeiten, d.h. z.B. LoadControl() ausführen.

Update:

Es ist kein Bug, bzw. keiner mehr. In der June CTP ist er jedenfalls wohl schon behoben. Ist halt doch noch alles etwas beta ;-)

Kommentare

  1. Raimond Brookman schrieb am Samstag, 13. August 2005 22:03:00 Uhr:

    Have you also tried using TemplateControl as a base class instead of Page for your Content Class? (this was also the case in my code sample)
    Because having a Page (RenderContent) as a Control inside a Page (the one with Page_Load) is probably not supported at all..
  2. Thomas schrieb am Sonntag, 14. August 2005 14:04:00 Uhr:

    Yes, I tried it - but with no success. I am now using the Page instance of the base page - that's of course a little bit dirty, but it works.

    I also heard the bug is since the June CTP fixed.


« Zurück  |  Weiter »