0

我正在尝试开发两个动态表,用户可以直接在第一个表中添加新行,当他完成添加后,他将能够选择它以打开第一个表下方的第二个表。我现在在选择第一个表中的行以显示第二个表时遇到了问题。那么如何获得呢?

仅供参考,我有以下数据库设计:

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 代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Table ID="questionsList" runat="server" Width="70%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366; font-size:17px; font-weight:bold;">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell>ID</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Question</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Question Order</asp:TableHeaderCell>
                        <asp:TableHeaderCell>Answer Explanation</asp:TableHeaderCell>
                    </asp:TableHeaderRow>
        </asp:Table>

        <asp:Table ID="Table1" runat="server" Width="70%" CellPadding="5" CellSpacing="0" 
                    style="border:2px solid #003366;">
            <asp:TableFooterRow>
                <asp:TableCell>Add</asp:TableCell>

                <asp:TableCell >
                    <asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox>

                    <%--For the txtQuestion TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server"
                                    TargetControlID="txtQuestion" WatermarkText="Type the Question here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:TextBox ID="txtQuestionOrder" runat="server"></asp:TextBox>

                    <%--For the txtQuestionOrder TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2" runat="server"
                                    TargetControlID="txtQuestionOrder" WatermarkText="Type the Question Order here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:TextBox ID="txtAnswerExplanation" runat="server"></asp:TextBox>

                    <%--For the txtAnswerExplanation TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender3" runat="server"
                                    TargetControlID="txtAnswerExplanation" WatermarkText="Type the Answer Explanation here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <span style="margin:3px 10px 0px 0px;">
                        <asp:ImageButton ID="addButton" runat="server" ImageUrl="Images/tick_small.png" OnClick="addQuestion" />
                    </span>
                </asp:TableCell>
            </asp:TableFooterRow>
        </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel> 

代码隐藏:

public void fill_Questions()
    {

        string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string selectQuery = "SELECT * FROM Question";
        SqlCommand cmd = new SqlCommand(selectQuery, conn);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;

        Table table = (Table)UpdatePanel1.FindControl("questionsList");
        try
        {
            table.Rows.Clear();
            //open the connection
            conn.Open();
            //filling the dataset with the data
            sda.Fill(ds);
            //close the connection
            conn.Close();
        }
        catch
        {
            table.Rows.Clear();
        }


        //Loading the data into the table
        if (ds.Tables.Count > 0)
        {
            int rowsNum = ds.Tables[0].Rows.Count;
            for (int i = 0; i < rowsNum; i++) 
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);

                for (int colCount = 0; colCount < 5; colCount++)
                {
                    TableCell cell = new TableCell();
                    row.Cells.Add(cell);

                    if (colCount == 0)
                    {
                        //int n = i + 1;
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["QuestionID"].ToString()));
                    }
                    else if (colCount == 1)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["Question"].ToString()));
                    }
                    else if (colCount == 2)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["QuestionOrder"].ToString()));
                    }
                    else if (colCount == 3)
                    {
                        cell.Controls.Add(new LiteralControl(ds.Tables[0].Rows[i]["AnswerExplanation"].ToString()));
                    }
                    else if (colCount == 4)
                    {
                        ImageButton deleteButton = new ImageButton();
                        deleteButton.ImageUrl = "Images/DeleteRed.png";
                        deleteButton.ID = ds.Tables[0].Rows[i]["QuestionID"].ToString() + "_" + i;
                        deleteButton.Click += new ImageClickEventHandler(deleteQuestion);
                        cell.Controls.Add(deleteButton);

                    }
                } //End for loop
            }//End OUTER for loop

        }//End if statement

    }


    //For deleting question
    public void deleteQuestion(object sender, EventArgs e) 
    {
        string id = ((ImageButton)sender).ID.ToString().Split('_')[0];
        try 
        {
            string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string deleteQuery = "DELETE FROM Question WHERE QuestionID = @id";
            SqlCommand cmd = new SqlCommand(deleteQuery,conn);
            cmd.Parameters.AddWithValue("@id", id);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            fill_Questions();
        }
        catch(SqlException se){}

        UpdatePanel1.Update();

    }


    //For adding questions
    public void addQuestion(object sender, EventArgs e) 
    {
        try 
        {
            TextBox txtQuestion = (TextBox)UpdatePanel1.FindControl("txtQuestion");
            TextBox txtQuestionOrder = (TextBox)UpdatePanel1.FindControl("txtQuestionOrder");
            TextBox txtAnswerExplanation = (TextBox)UpdatePanel1.FindControl("txtAnswerExplanation");

            string connString = ConfigurationManager.ConnectionStrings["QuizSysDBConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string insertQuery = "INSERT INTO Question (Question, QuestionOrder, AnswerExplanation) VALUES (@Question, @QuestionOrder, @AnswerExplanation)";
            SqlCommand cmd = new SqlCommand(insertQuery,conn);
            cmd.Parameters.AddWithValue("@Question", txtQuestion.Text);
            cmd.Parameters.AddWithValue("@QuestionOrder", txtQuestionOrder.Text);
            cmd.Parameters.AddWithValue("@AnswerExplanation", txtAnswerExplanation.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            fill_Questions();
        }
        catch(SqlException se){}
    }

更新:

我为答案创建了两个表格,就像我为问题所做的那样。现在,在选择一个问题后,我将如何显示这些表格?

ASP.NET 代码:

<div align="center">
    <h3>Answers List</h3>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Table ID="answersList" runat="server" Width="70%" CellPadding="0" CellSpacing="0"
                    style="border:2px solid #003366; font-size:17px; font-weight:bold;">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell>Answer</asp:TableHeaderCell>
                    </asp:TableHeaderRow>
        </asp:Table>

        <asp:Table ID="Table2" runat="server" Width="70%" CellPadding="5" CellSpacing="0" 
                    style="border:2px solid #003366;">
            <asp:TableFooterRow>
                <asp:TableCell>Add</asp:TableCell>

                <asp:TableCell >
                    <asp:TextBox ID="txtAnswer" runat="server"></asp:TextBox>

                    <%--For the txtQuestion TextBox Control--%>
                    <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender4" runat="server"
                                    TargetControlID="txtAnswer" WatermarkText="Type the Answer here"
                                    WatermarkCssClass="watermarked">
                    </ajaxToolkit:TextBoxWatermarkExtender>
                </asp:TableCell>

                <asp:TableCell>
                    <asp:CheckBox ID="isCorrectCheckBox" runat="server" />
                </asp:TableCell>

                <asp:TableCell>
                    <span style="margin:3px 10px 0px 0px;">
                        <asp:ImageButton ID="ImageButton1" runat="server" ToolTip="Add" ImageUrl="Images/tick_small.png" OnClick="addAnswer" />
                    </span>
                </asp:TableCell>
            </asp:TableFooterRow>
        </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel> 
    </div>
4

0 回答 0