Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are children of my custom user-control not being initialized?
    primarykey
    data
    text
    <p><strong>UPDATE:</strong> <em>I've been experimenting with my controls some more and I think I've gotten closer. Please read on for updated info.</em></p> <p>I have 2 ASP.NET 2.0 user controls, one of which is placed <em>inside</em> of the other one. Inside of the inner control I have an <code>HtmlAnchor</code> tag control which I'm trying to set a URL for from the outer control. When I attempt to set the <code>HRef</code> property from the rendering page's markup, the <code>HtmlAnchor</code> control is <code>null</code> and throws an exception.</p> <p>The URL <code>set</code> property is being called <em>before</em> the OnInit event of either the inner or the outer control and before the main Page's '<code>OnPreInit</code>' event. I'm assuming this is because I am setting the URL of the child control in the markup of the parent control on the page I'm rendering the controls on which means the value is set before <code>OnInit()</code>. Here is the (stripped down) version of the code I'm using: </p> <pre><code>[ParseChildren(true)]// &lt;- Setting this to false makes the // page render without an exception // but ignores anything in the markup. [PersistChildren(false)] public partial class OuterControl : System.Web.UI.UserControl { // The private '_Inner' control is declared // in the .ascx markup of the control [PersistenceMode(PersistenceMode.InnerProperty)] public InnerControl Inner { get{ return _Inner; } set{ _Inner = value; } } } public partial class InnerControl : System.Web.UI.UserControl { // The private 'linkHref' control is declared // in the .ascx markup of the control public string Url { get { return linkHref.HRef; } set { linkHref.HRef = value; } } } </code></pre> <p>The OuterControl is used on my Default.aspx page like this:</p> <pre><code>&lt;uc1:OuterControl ID="OuterCtrl1" runat="server"&gt; &lt;Inner Url="#" /&gt; &lt;/uc1:OuterControl&gt; </code></pre> <p>In the markup example above, if I try to render this page an exception gets thrown because the <code>linkHref</code> control is null. Using my debugger I can see that every control within the InnerControl is null, but both the InnerControl &amp; OuterControl's <code>OnInit()</code> event has not been triggered yet when the <code>Url</code> property is accessed.</p> <p><strong>UPDATE</strong><br> I thought adding the attributes '<code>ParseChildren</code>' and '<code>PersistChildren</code>' would help. I've used them in Server Controls before but never in User Controls, although the effect seems to be similar. I don't think I'm interpreting the documentation for these two properties correctly, but it can stop exceptions from being thrown. The page markup becomes ignored though.</p> <p>Does anyone know a way to have this work. I don't understand why these controls are getting values set before <code>OnInit()</code>. When I try to set their values using the ASPX markup, the constructor for the <code>InnerControl</code> is being called twice. Once to set the values based on the markup (I'm assuming) and again on <code>OnInit()</code> (which I'm guessing is why the markup values are getting ignored).</p> <p>Is this effort hopeless or am I just approaching it from the wrong angle?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload