Hello all.
I am currently working on a website, created using visual studio, .NET and the C# language.
I am using a detailsview as a data entry interface.
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.
The problem is: When I attempt to dynamically add these checkbox controls while the detailsview is in "readonly" mode, I cannot reference the placeholder in the detailsview itemtemplate the checkboxes are to be added to. The error I am receiving is:
"System.NullReferenceException: Object reference not set to an instance of an object."
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, only those that are currently set.
Here is the detailsview template field:
<asp:TemplateField HeaderText="Displayed in: " SortExpression="displayInFeaturesPanel">
<ItemTemplate>
<asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"></asp:PlaceHolder>
</ItemTemplate>
<InsertItemTemplate>
<asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"></asp:PlaceHolder>
</InsertItemTemplate>
<EditItemTemplate>
<asp:PlaceHolder ID="displaySettingsPlaceHolder" runat="server"></asp:PlaceHolder>
</EditItemTemplate>
</asp:TemplateField>
..and the section of the DetailsView_ItemDatabound method where I attempt to reference the placeholder (checkbox generation code omitted for brevity):
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);
}
Can anyone offer advice on this problem?
Thank you in advance for your help.