I am creating a simple form. I have created two simple classes, Question and QuestionSection.
I then create some subclasses of question, e.g. SimpleQuestion, MultipleChoiceQuestion, with different behaviour and markup.
QuestionSection has a property of a collection of questions, like so-
private List<Question> _Questions = new List<Question>();
public List<Question> Questions
{
get { return _Questions; }
}
which is set on, say, SurveyForm.aspx like so-
<uc:QuestionSection ID="ucQuestionSection" runat="server">
<questions>
<uc:SimpleQuestion ID="qFirstName" runat="server" QuestionText="First name" />
<uc:SimpleQuestion ID="qLastName" runat="server" QuestionText="Last name" />
<uc:MultipleChoiceQuestion ID="qEmail" runat="server" QuestionText="Email address" />
</questions>
</uc:QuestionSection>
On my QuestionSection.ascx page I have-
<div class="portlet-section-body form-fields">
<asp:Panel ID="pnlQuestions" runat="server" ></asp:Panel>
</div>
which is load by this function on QuestionSection.ascx.cs
protected void Page_Init(object sender, EventArgs e)
{
foreach (Question q in Questions)
{
pnlQuestions.Controls.Add(q);
}
}
I have a couple of questions-
- Is this a good idea?
- I am having some problems around postback and persistance of controls. My thinking is that these question controls are not dynamic, and so should work. However, on postback the question controls are not shown.
- Is there a better control to use in QuestionSection than the Panel control? I considered just using this.Controls.Add, but I can't see how I can add a div on either side of the control collection.