I am a new ASP.NET developer and I am trying to develop a simple Quiz Engine that will allow the System Administrator to create quizzes. What I did is the following: I created a ListView for inserting the Quiz title and description, and for the content of the quiz itself like the questions and the answers, I have a big problem with that. Since each quiz has different number of questions. And each question has different number of answers since the question could be multiple choice question with four answers and it could be True of False question. So what is the best why to accomodate all of these requirements.? Any idea? And could you please provide me with example or a code snippet?
FYI, I have the following database design:
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
QuestionImage Table: ID, QuestionID, URL
Answer Table: AnswerID, Answer
QuizContent Table: ID, QuizID, QuestionID, AnswerID
My ASP.NET code:
<div align="center">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="QuizID"
DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >
<EditItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" Width="20px" runat="server" CommandName="Update" />
<asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" Width="20px" runat="server" CommandName="Cancel" />
</td>
<td>
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="InsertButton" ImageUrl="Images/icons/create 2 48.png" Width="20px" runat="server" CommandName="Insert" />
<asp:ImageButton ID="CancelButton" ImageUrl="images/clear3.png" Width="20px" runat="server" CommandName="Cancel" />
</td>
<%--<td>
</td>--%>
<td>
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" Width="20px" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit224.png" Width="20px" runat="server" CommandName="Edit" />
<asp:ImageButton ID="SelectButton" ImageUrl="images/select.png" Width="20px" runat="server" CommandName="Select" />
<%--<asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Select" />--%>
</td>
<%--<td>
<asp:Label ID="QuizIDLabel" runat="server"
Text='<%# Eval("QuizID") %>' />
</td>--%>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
<thead>
<tr style="background-color:#C6D7B5;">
<th style="border-bottom:2px solid #003366; ">...</th>
<th style="border-bottom:2px solid #003366; ">Title</th>
<th style="border-bottom:2px solid #003366; ">Description</th>
</tr>
</thead>
<tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
</table></div>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:ImageButton ID="DeleteButton" ImageUrl="images/delete24.png" Width="20px" runat="server" CommandName="Delete" />
<asp:ImageButton ID="EditButton" ImageUrl="images/edit224.png" Width="20px" runat="server" CommandName="Edit" />
</td>
<%--<td>
<asp:Label ID="QuizIDLabel" runat="server"
Text='<%# Eval("QuizID") %>' />
</td>--%>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>"
SelectCommand="SELECT * FROM [Quiz]"
DeleteCommand="DELETE FROM [Quiz] WHERE [QuizID] = @QuizID"
InsertCommand="INSERT INTO [Quiz] ([Title], [Description]) VALUES (@Title, @Description)"
UpdateCommand="UPDATE [Quiz] SET [Title] = @Title, [Description] = @Description WHERE [QuizID] = @QuizID">
<DeleteParameters>
<asp:Parameter Name="QuizID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Description" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="QuizID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
UPDATE:
I am trying to make the quiz engine dynamic as much as possible. Since I have different number of possible answers for each question, I want the user to enter the number of answers first, and the textboxes will be created for him. I already wrote the code but it did not work. There is no error but there is no TextBoxes created for me. So how to do that?
My ASP.NET code (I created a panel but I never used it):
<div>
<asp:Label ID="lblText" runat="server" Text="How many answers?" />
<asp:TextBox ID="NumOfTextBoxes" runat="server"></asp:TextBox>
<asp:Button ID="insertAnswerTextBox" runat="server" Text="Enter" OnClick="insertAnswerTextBox_OnClick" />
<asp:Panel ID="AnswersForm" runat="server" Visible="false">
<asp:TextBox ID="AnswerTextBox" runat="server"></asp:TextBox>
</asp:Panel>
</div>
Code-Behind:
protected void insertAnswerTextBox_OnClick(object sender, EventArgs e)
{
PlaceHolder PlaceHolder1 = new PlaceHolder();
// Get the number of textbox to create.
int number = System.Convert.ToInt32(NumOfTextBoxes.Text);
for (int i = 1; i <= number; i++)
{
TextBox AnswerTextBox = new TextBox();
// Set the label's Text and ID properties.
AnswerTextBox.Text = "AnswerTxtBox" + i.ToString();
AnswerTextBox.ID = "AnswerTxtBox" + i.ToString();
PlaceHolder1.Controls.Add(AnswerTextBox);
// Add a spacer in the form of an HTML <br /> element.
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}
Your code is correct except one thing you forgot to add. You added all the textboxes to the placeholder control but you have not added place holder to your web form. Add the following line to your
insertAnswerTextBox_OnClick.Complete code should look like this