2014-02-09 69 views
2

如何通過基於html表格的循環來關閉它們是否可見,併爲每個循環分配不同的參數值。不知道這是否是解決此問題的正確方法,但希望獲得一些建議和一些示例代碼,以便儘可能實現以下目標。根據循環更改參數值

表例如

Four of these tables so for the second table I have the id="txtLName1" 
    <table id="tblEvent1" class="tblAttendee"> 
       <tr> 
        <td>Last Name:</td> 
        <td><input id="txtLName" runat="server" type="text" name="name" /></td> 
       </tr> 
       <tr> 
        <td>First Name:</td> 
        <td><input id="txtFName" runat="server" type="text" name="name" /></td> 
       </tr> 
       <tr> 
        <td>Gender:</td> 
        <td><asp:DropDownList ID="ddlGender" runat="server" Width="65%"> 
         </asp:DropDownList></td> 
       </tr> 
       <tr> 
        <td>Age:</td> 
        <td><asp:DropDownList ID="ddlAge" runat="server" Width="65%"> 
         </asp:DropDownList></td> 
       </tr> 
       <tr> 
        <td>Comments:</td> 
        <td><textarea id="txtComments" runat="server" type="text"></textarea></td> 
       </tr> 
      </table> 

Table1 - visible = true 
Table2 - visible = true 
Table3 - visible = true 
Table4 - visible = false 

    using (SqlConnection con = new SqlConnection(connectionString)) 
     { 
      using (SqlCommand cmd = con.CreateCommand()) 
      { 

       cmd.CommandText = "StoredProcedureName"; 
       cmd.CommandType = CommandType.StoredProcedure; 

Loop through tables where visible = true 
//first loop 
cmd.Parameters.AddWithValue("@LastName", Request.Form["(Table1.LastName"]); 
insert new record to database 
//second loop 
cmd.Parameters.AddWithValue("@LastName", Request.Form["Table2.LastName"]); 
insert new record to database using same storedprocedure call 
//third loop 
cmd.Parameters.AddWithValue("@LastName", Request.Form["Table3.LastName"]); 
insert new record to database using same storedprocedure call 
No 4th loop because Table is not visible. 

到底數據庫表應該使用相同的存儲過程,但循環,所以它會被多次調用它像這樣在這種情況下,插入3條記錄

(LastName)Smith (firstName)Bob (gender)Male (age)13 (linkid)1 
(LastName)Smith (firstName)Timmy (gender)Male (age)7 (linkid)1 
(LastName)Smith (firstName)Jenny (gender)Female (age)9 (linkid)1 
+0

您使用的web表單或MVC空?頁面上的代碼是什麼樣的?表如何填充? – TomT

+0

我添加了一個表TomT的例子,這是webforms asp.net C#,並且sql表將基於列出的字段進行填充。也許我沒有清楚地問這個問題,但我試圖依靠在我的網頁循環中顯示多少表,通過將第一個表插入到數據庫中,然後循環將第二個表插入爲新記錄,等等 – Tim

回答

0

改變了SQL存儲過程來接受所有參數,但只插入表中,如果他們不