0

我是一名新的 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>
                            &nbsp;</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>

更新:

我正在尝试使测验引擎尽可能动态化。由于每个问题我有不同数量的可能答案,我希望用户先输入答案的数量,然后为他创建文本框。我已经写了代码,但是没有用。没有错误,但没有为我创建文本框。那么该怎么做呢?

我的 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 />"));
        }
    }
4

1 回答 1

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); 
}
于 2012-07-17T12:27:40.097 回答