Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make the second table appears after selecting a row in the first table?
    primarykey
    data
    text
    <p>I am a new ASP.NET developer and 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 problem now in selecting on the rows in the first table in order to show the second table. <strong>So how to get it?</strong> </p> <p>FYI, I have the following database design:</p> <pre><code>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 </code></pre> <p>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.</p> <p>ASP.NET Code:</p> <pre><code>&lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;asp:Table ID="questionsList" runat="server" Width="70%" CellPadding="0" CellSpacing="0" style="border:2px solid #003366; font-size:17px; font-weight:bold;"&gt; &lt;asp:TableHeaderRow&gt; &lt;asp:TableHeaderCell&gt;ID&lt;/asp:TableHeaderCell&gt; &lt;asp:TableHeaderCell&gt;Question&lt;/asp:TableHeaderCell&gt; &lt;asp:TableHeaderCell&gt;Question Order&lt;/asp:TableHeaderCell&gt; &lt;asp:TableHeaderCell&gt;Answer Explanation&lt;/asp:TableHeaderCell&gt; &lt;/asp:TableHeaderRow&gt; &lt;/asp:Table&gt; &lt;asp:Table ID="Table1" runat="server" Width="70%" CellPadding="5" CellSpacing="0" style="border:2px solid #003366;"&gt; &lt;asp:TableFooterRow&gt; &lt;asp:TableCell&gt;Add&lt;/asp:TableCell&gt; &lt;asp:TableCell &gt; &lt;asp:TextBox ID="txtQuestion" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;%--For the txtQuestion TextBox Control--%&gt; &lt;ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="txtQuestion" WatermarkText="Type the Question here" WatermarkCssClass="watermarked"&gt; &lt;/ajaxToolkit:TextBoxWatermarkExtender&gt; &lt;/asp:TableCell&gt; &lt;asp:TableCell&gt; &lt;asp:TextBox ID="txtQuestionOrder" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;%--For the txtQuestionOrder TextBox Control--%&gt; &lt;ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2" runat="server" TargetControlID="txtQuestionOrder" WatermarkText="Type the Question Order here" WatermarkCssClass="watermarked"&gt; &lt;/ajaxToolkit:TextBoxWatermarkExtender&gt; &lt;/asp:TableCell&gt; &lt;asp:TableCell&gt; &lt;asp:TextBox ID="txtAnswerExplanation" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;%--For the txtAnswerExplanation TextBox Control--%&gt; &lt;ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender3" runat="server" TargetControlID="txtAnswerExplanation" WatermarkText="Type the Answer Explanation here" WatermarkCssClass="watermarked"&gt; &lt;/ajaxToolkit:TextBoxWatermarkExtender&gt; &lt;/asp:TableCell&gt; &lt;asp:TableCell&gt; &lt;span style="margin:3px 10px 0px 0px;"&gt; &lt;asp:ImageButton ID="addButton" runat="server" ImageUrl="Images/tick_small.png" OnClick="addQuestion" /&gt; &lt;/span&gt; &lt;/asp:TableCell&gt; &lt;/asp:TableFooterRow&gt; &lt;/asp:Table&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>Code-Behind:</p> <pre><code>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 &gt; 0) { int rowsNum = ds.Tables[0].Rows.Count; for (int i = 0; i &lt; rowsNum; i++) { TableRow row = new TableRow(); table.Rows.Add(row); for (int colCount = 0; colCount &lt; 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){} } </code></pre> <p><strong>UPDATE:</strong></p> <p>I created two tables for the Answers as I did for the questions. Now, <strong>How Am I gonna be able to show these table after selecting one question?</strong></p> <p>ASP.NET code:</p> <pre><code>&lt;div align="center"&gt; &lt;h3&gt;Answers List&lt;/h3&gt; &lt;asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;asp:Table ID="answersList" runat="server" Width="70%" CellPadding="0" CellSpacing="0" style="border:2px solid #003366; font-size:17px; font-weight:bold;"&gt; &lt;asp:TableHeaderRow&gt; &lt;asp:TableHeaderCell&gt;Answer&lt;/asp:TableHeaderCell&gt; &lt;/asp:TableHeaderRow&gt; &lt;/asp:Table&gt; &lt;asp:Table ID="Table2" runat="server" Width="70%" CellPadding="5" CellSpacing="0" style="border:2px solid #003366;"&gt; &lt;asp:TableFooterRow&gt; &lt;asp:TableCell&gt;Add&lt;/asp:TableCell&gt; &lt;asp:TableCell &gt; &lt;asp:TextBox ID="txtAnswer" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;%--For the txtQuestion TextBox Control--%&gt; &lt;ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender4" runat="server" TargetControlID="txtAnswer" WatermarkText="Type the Answer here" WatermarkCssClass="watermarked"&gt; &lt;/ajaxToolkit:TextBoxWatermarkExtender&gt; &lt;/asp:TableCell&gt; &lt;asp:TableCell&gt; &lt;asp:CheckBox ID="isCorrectCheckBox" runat="server" /&gt; &lt;/asp:TableCell&gt; &lt;asp:TableCell&gt; &lt;span style="margin:3px 10px 0px 0px;"&gt; &lt;asp:ImageButton ID="ImageButton1" runat="server" ToolTip="Add" ImageUrl="Images/tick_small.png" OnClick="addAnswer" /&gt; &lt;/span&gt; &lt;/asp:TableCell&gt; &lt;/asp:TableFooterRow&gt; &lt;/asp:Table&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload