2012-07-18 49 views
0

我想開發一個動態表,用戶可以直接添加新行。我現在在代碼隱藏中獲得插入問題的ID,但我不知道如何獲取它。那麼如何得到它?如何獲取ID以從表格中刪除該項目?

僅供參考,數據庫設計,我已是如下: 問表:QuestionID,問題,QuestionOrder,AnswerExplanation

ASP.NET代碼:

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

     <asp:Table ID="Table1" runat="server" Width="50%" CellPadding="0" CellSpacing="0" 
        style="border:2px solid #003366;"> 
      <asp:TableFooterRow> 
       <asp:TableCell>Add</asp:TableCell> 
       <asp:TableCell> 
        <asp:TextBox ID="txtQuestion" runat="server"></asp:TextBox> 
        <ajaxToolkit:TextBoxWatermarkExtender ID="TBWE" runat="server" 
            TargetControlID="Question" WatermarkText="Type the Question 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 Questions"; 
     SqlCommand cmd = new SqlCommand(selectQuery, conn); 
     DataSet ds = new DataSet(); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     sda.SelectCommand = cmd; 

     Table table = (Table)UpdatePanel1.FindControl("questionsList"); 
     try 
     { 
      //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 < 4; colCount++) 
       { 
        TableCell cell = new TableCell(); 
        row.Cells.Add(cell); 

        if (colCount == 0) 
        { 
         int n = i + 1; 
         cell.Controls.Add(new LiteralControl(n.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]["Question"].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) 
    { 
     //int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split()[0]); 
     try 
     { 
      string connString = ConfigurationManager.ConnectionStrings["QuizSysDB"].ConnectionString; 
      SqlConnection conn = new SqlConnection(connString); 
      string deleteQuery = "DELETE FROM Question WHERE ID = @id"; 
      SqlCommand cmd = new SqlCommand(deleteQuery,conn); 
      cmd.Parameters.AddWithValue("@id", ID); 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 
      fill_Questions(); 
     } 
     catch(SqlException se){} 
    } 

我現在的問題與deleteQuestion方法中的下面一行完全相同:

//int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split()[0]); 
+2

請回滾到原來的,有人已經哪種足以提供一個回答你的問題。如果你想問另一個問題,那麼提出一個新問題。這個網站也是關於幫助他人的,如果別人看不到正確的問題,他們不會找到他們需要的答案。 – musefan 2012-07-18 09:58:59

回答

0

你可以得到的ImageButton id列這樣

int id = System.Convert.ToInt32((ImageButton)sender.ToString().Split('_')[1]); 
+0

抱歉,錯誤的描述。你能看看我更新的問題嗎? – 2012-07-18 09:19:56

+0

@TechLover您更新的問題與您最初的問題完全不同 – HatSoft 2012-07-18 09:28:53

+1

對不起,因爲我有兩個問題,而不是發佈這個問題,所以我發佈了第一個問題,我在發佈問題的同時發佈了第一個問題。 – 2012-07-18 09:37:28