2014-02-09 68 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存储过程来接受所有参数,但只插入表中,如果他们不