Can .NET controls be added to a detailsview that is in Readonly mode (during databound event?)
<p>I am currently working on a website, created using visual studio, .NET and the C# language.</p>

<p>I am using a detailsview as a data entry interface.</p>

<p>In the detailsview "edit" and "insert" modes, I am dynamically adding checkbox controls
which represent the boolean field values set for the current record displayed in the detailsview. I wish to do the same in "readonly" mode of the detailsview.</p>

<p>The problem is: When I attempt to dynamically add these checkbox controls while the detailsview is in "readonly" mode, <strong>I cannot reference the placeholder in the detailsview itemtemplate the checkboxes are to be added to</strong>. The error I am receiving is:</p>

<p>"System.NullReferenceException: Object reference not set to an instance of an object."</p>

<p>Each record the detailsview is to display can have a differing number of boolean fields (and therefore checkboxes) to be generated. Using declaratively added boundfields is not an option, as I do not want to display all of the possible checkbox fields that can be associated with a record, <strong>only those that are currently set</strong>.</p>

<p>Here is the detailsview template field:</p>

<pre><code>&lt;asp:TemplateField HeaderText="Displayed in: " SortExpression="displayInFeaturesPanel"&gt;   
    &lt;ItemTemplate&gt;
       &lt;asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
    &lt;/ItemTemplate&gt;
    &lt;InsertItemTemplate&gt;
       &lt;asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
    &lt;/InsertItemTemplate&gt;
    &lt;EditItemTemplate&gt;
       &lt;asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
    &lt;/EditItemTemplate&gt;
&lt;/asp:TemplateField&gt;
</code></pre>

<p>..and the section of the DetailsView_ItemDatabound method where I attempt to reference the placeholder (checkbox generation code omitted for brevity):</p>

<pre><code>if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
{
    // Generate controls in PlaceHolder
    PlaceHolder subGroupsPlaceHolder = (PlaceHolder)DetailsView1.FindControl(controlID2);

    Label testLbl = new Label();
    testLbl.Text = "Control added to placeholder";
    testLbl.Visible = true;                                   
    subGroupsPlaceHolder.Controls.Add(testLbl);
}
</code></pre>

<p>Can anyone offer advice on this problem?</p>

<p>Thank you in advance for your help.</p>
