0

私は新しい ASP.NET 開発者であり、ユーザーが最初のテーブルに新しい行を直接追加できる 2 つの動的テーブルを開発しようとしています。追加が完了したら、それを選択して最初のテーブルの下にある 2 番目のテーブルを開くことができます。テーブル。2 番目のテーブルを表示するために、最初のテーブルの行を選択する際に問題が発生しました。では、どうやって手に入れるのですか?

参考までに、次のデータベース設計があります。

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

さらに、最初のテーブルは質問を挿入するためのもので、2 番目のテーブルは回答を挿入するためのものです。では、質問を選択して 2 番目のテーブルを表示し、その質問の回答を挿入する方法を説明します。

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

アップデート:

質問と同じように、回答用に 2 つのテーブルを作成しました。さて、1 つの質問を選択した後、これらの表を表示するにはどうすればよいでしょうか?

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