如何显示第二个表格以插入已插入第一个表格中的问题的答案? [英] How to show the second table for inserting answers of the questions that have been inserted in the first table?

查看:81
本文介绍了如何显示第二个表格以插入已插入第一个表格中的问题的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I am trying to develop two dynamic tables where the user can add a new row directly in the first table, when he finished adding it, he will be able to select it to open the second table underneath the first table. I have a probelm now in selecting on the rows in the first table in order to show the second table. So how to get it?

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



此外,第一个表用于插入问题,第二个表用于插入答案.因此,我如何选择一个问题以显示第二个表格并能够插入该问题的答案

ASP.NET代码:



In addition, the first table will be for inserting the questions and the second table will be for inserting the answers. So how I can select a question to show the second table and being able to insert the answers for that question

ASP.NET Code:

<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>



后台代码:



Code-Behind:

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



**UPDATE:**

I created two tables for the Answers as I did for the questions. Now, **how am I gonna be able to show these table after selecting one question?**

ASP.NET code:

<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>

推荐答案

听起来像是主要细节"解决方案,

http://msdn.microsoft.com/en-us/library/aa581796.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/aa479344.aspx [ ^ ]
Sounds like a "master detail" solution to me,

http://msdn.microsoft.com/en-us/library/aa581796.aspx[^]

or

http://msdn.microsoft.com/en-us/library/aa479344.aspx[^]


这篇关于如何显示第二个表格以插入已插入第一个表格中的问题的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆