我是一名新的ASP.NET开发人员,我正在尝试开发一个简单的测验引擎,允许系统管理员创建测验。我做的是以下内容:我创建了一个ListView用于插入测验标题和描述,对于测验本身的内容,如问题和答案,我有一个很大的问题。由于每个测验都有不同数量的问题。每个问题都有不同的答案数量,因为问题可能是四个答案的多项选择问题,它可能是真假的问题。那么满足所有这些要求的最佳原因是什么呢?任何的想法?你可以请我提供示例或代码片段吗?
仅供参考,我有以下数据库设计:
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
我的ASP.NET代码:
<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>
更新
我试图让测验引擎尽可能动态。由于每个问题都有不同数量的可能答案,我希望用户先输入答案数,然后为他创建文本框。我已经编写了代码,但它没有用。没有错误,但没有为我创建TextBox。 那怎么做?
我的ASP.NET代码(我创建了一个面板,但我从未使用过它):
<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>
代码隐藏:
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 />"));
}
}
答案 0 :(得分:1)
您的代码是正确的,除了您忘记添加的一件事。您已将所有文本框添加到占位符控件,但尚未将占位符添加到Web表单中。将以下行添加到insertAnswerTextBox_OnClick
。
form1.Controls.Add(PlaceHolder1);
完整代码应如下所示
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 />"));
}
form1.Controls.Add(PlaceHolder1);
}