如何在第一个表中选择一行后显示第二个表?

时间:2012-07-21 04:29:39

标签: c# asp.net sql-server-2008-r2

我是一名新的ASP.NET开发人员并尝试开发两个动态表,用户可以在第一个表中直接添加新行,当他完成添加后,他将能够选择它来打开第二个表在第一张桌子下面。我现在在选择第一个表中的行以显示第二个表时遇到问题。 那么如何获得它?

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

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){}
    }

更新

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

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>

0 个答案:

没有答案