1

Please provide any useful links.

I need to create a sample question types web form in VB.NET which allow user to the following:

  1. The user selects the control type from dropdown (TextBox, RadioButton, ListBox etc).
  2. Generate controls dynamically based on the control type on the webform.
  3. It will always show the TextBox (where user writes the question) and (generated control - TextBox, RadioButton, ListBox etc) and save those values to the database.

  4. Display generated sample questions on the web form.

Thanks.

2
  • What does that mean: "The user write questions, and save those generated questions to the database." ? How would you fill controls like ListBox when all you know is that the uer wants a ListBox? Jun 12, 2012 at 19:20
  • I think you're going to be looking at two different UIs to build. The first is going to be creating a UI that will allow you to create and manage the questions and possible answers. The second will be how to take the data that is stored and render out a form for user interaction to answer the questions.
    – Nick Bork
    Jun 12, 2012 at 20:17

3 Answers 3

0

something like this should work (without database interaction ;)).

aspx:

<asp:DropDownList runat="server" id="TypeDropDown" OnSelectedIndexChanged="OnTypeChanged">
  <asp:ListItem>TextBox</asp:ListItem>
  <asp:ListItem>RadioButton</asp:ListItem>
</asp:DropDownList>
<asp:Panel>
    Question: <asp:TextBox runat="server" ID="Question" /> <br />
    Answer: <asp:PlaceHolder runat="server" ID="Place" />
</asp:Panel>

code-behind:

protected override void OnInit(EventArgs eventArgs) {
  base.OnInit(eventArgs);
  CreateDynamicControl(); 
}

private void CreateDynamicControl() {
  Place.Controls.Clear();
  Place.Controls.Add(ControlFactory.Create(TypeDropDown.SelectedValue);
}

private void OnTypeChanged(object sender, EventArgs eventArgs) {
  CreateDynamicControl();
}

control factory:

static class ControlFactory {
  public static Control Create(string type) {
    if ("TextBox".Equals(type))
      return new TextBox();

    if ("RadioButton".Equals(type)) 
      return new RadioButtonList();
  }
}
0

I think what you are looking for is a DBMS solution where you save in the DB the type of control and than when it is time to show the page you are looking at the type and using a placeholder to generate what you need. here is a link to wikipedia that explains DBMS : http://en.wikipedia.org/wiki/Database_management_system

0

I did something similar to this using Declarative syntax, using DataBinding.

Sorry the code is so long, but posting JUST the relevant portion without the nested repeaters might not make sense. I'm posting the full Nested Repeater code for context.

In my setup, this is a survey app, with Question Groups (outside repeater), comprised of one or more Questions (nested repeater).

The relevant portion of how this works is in the nested repeater in the code sample. I had all of the controls available in the markup,. but the .Visible property of each was set based on the QuestionType.

    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <div class="questiongroup">
                <asp:HiddenField runat="server" ID="lblQuestionGroupId" Value='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "QuestionGroupId").ToString()) %>'>
                </asp:HiddenField>
                <asp:HiddenField runat="server" ID="hfSortOrder" Value='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "SortOrder").ToString()) %>'>
                </asp:HiddenField>
                <asp:HiddenField runat="server" ID="hdnPointsAwarded" Value='0'></asp:HiddenField>
                <br />
                <h3><asp:Label runat="server" ID="txtQuestionGroupName" MaxLength="50" Columns="50" Text='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "QuestionGroupName").ToString()) %>'></asp:Label>
                </h3>
                Score Section
                <asp:CheckBox runat="server" ID="chkIsScoreSection" Enabled="false" TabIndex="-1"
                    Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsScoreSection")) %>' />
                Minimum required correct answers:
                <asp:Label runat="server" ID="lblMinimumForScore" MaxLength="3" Columns="3" Text='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "CommentsRequiredMinimumYesAnswers").ToString()) %>'></asp:Label>
                  Point Value <asp:Label ID="lblPossiblePoints" runat="server" Text='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "PossiblePoints").ToString()) %>' />
                          <br />
                Group Instructions
                <br />
                <asp:Label runat="server" ID="lblGroupInstructions" TextMode="MultiLine" Columns="50" Rows="3"
                    Text='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "GroupInstructions").ToString()) %>'></asp:Label>
                <br />
                <div class='questionseditor'>
                </div>
                <br />
                <div class="questionsdiv">
                    <asp:Repeater ID="childRepeater" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("Relation2") %>'>
                        <ItemTemplate>
                        <div class="question">
                            <asp:HiddenField ID="hdnQuestionType" runat="server" Value='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) %>' />
                            <asp:HiddenField ID="hdnQuestionId" runat="server" Value='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionId\"]").ToString()) %>' />
                            <asp:HiddenField ID="hfQuestionSortOrder" runat="server" Value='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"SortOrder\"]").ToString()) %>' />
                            <asp:RequiredFieldValidator SetFocusOnError="True" ID="YesNoForScoreRequiredFieldValidator" runat="server"
                                ControlToValidate="lstYesNoForScore" Display="Dynamic" ErrorMessage="Required<br />"
                                Enabled='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "1" %>'></asp:RequiredFieldValidator>
                            <asp:RequiredFieldValidator SetFocusOnError="True" ID="MemoRequiredFieldValidator" runat="server" ControlToValidate="txtMemoAnswer"
                                Display="Dynamic" ErrorMessage="Required<br />" 
                                Enabled='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "2" %>'></asp:RequiredFieldValidator>
                            <asp:RequiredFieldValidator SetFocusOnError="True" ID="NumericAnswerRequiredFieldValidator" runat="server"
                                ControlToValidate="txtNumericAnswer" Display="Dynamic" ErrorMessage="Required<br />"
                                Enabled='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "3" %>'></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator SetFocusOnError="True" Display="Dynamic" runat="server" ID="NumericTextRegexValidator"
                                ValidationExpression="^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$" Enabled='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "3" %>'
                                ErrorMessage="*Invalid<br />" ControlToValidate="txtNumericAnswer"></asp:RegularExpressionValidator>
                            <asp:RequiredFieldValidator SetFocusOnError="True" ID="RequiredFieldValidator1" runat="server" ControlToValidate="lstYesNoNonScored"
                                Display="Dynamic" ErrorMessage="Required<br />" Enabled='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "5" %>'></asp:RequiredFieldValidator>
                            <asp:Label ID="lblQuestionText" runat="Server" Text='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionText\"]").ToString()) %>'></asp:Label><br />
                            <asp:RadioButtonList runat="server" ID="lstYesNoForScore" RepeatDirection="Horizontal"
                                Visible='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "1" %>'>
                                <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
                                <asp:ListItem Text="No *" Value="0"></asp:ListItem>
                            </asp:RadioButtonList>
                            <asp:TextBox ID="txtMemoAnswer" runat="server" Visible='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "2" %>'
                                TextMode="MultiLine" Rows="3" Width="100%"></asp:TextBox>
                            <asp:TextBox ID="txtNumericAnswer" runat="server" Visible='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "3" %>'
                                cols="5"></asp:TextBox>
                            <uc1:MultipleChoiceControl ID="MultipleChoiceControl1" runat="server" Visible='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "4" %>'
                                QuestionId='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "[\"QuestionId\"]")) %>' />
                            <asp:RadioButtonList runat="server" ID="lstYesNoNonScored" RepeatDirection="Horizontal"
                                Visible='<%#  Microsoft.Security.Application.AntiXss.GetSafeHtmlFragment(  DataBinder.Eval(Container.DataItem, "[\"QuestionTypeId\"]").ToString()) == "5" %>'>
                                <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
                                <asp:ListItem Text="No" Value="0"></asp:ListItem>
                            </asp:RadioButtonList>
                            <br />
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
                Comments for this Question Group (Required if score not awarded)
                <asp:TextBox ID="txtGroupComments" runat="server" TextMode="MultiLine" Rows="3" Width="100%"></asp:TextBox>
            </div>
        </ItemTemplate>
    </asp:Repeater>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.