求解,为什么删除后只执行一次?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title> <script> function AddRow() { var tname = document.getElementById("t_x1").value; for(var i =1;i<=tname;i++) { var newTr = customers.insertRow(); var newTd = newTr.insertCell(); newTd.innerHTML = '<input type="text" id="t_b1' + i + '" name="t_b1' + i + '"/>'; newTd = newTr.insertCell(); newTd.innerHTML = '<input type="submit" id="box" value="删除" onclick="removeRow()" />'; } } function removeRow() { var rowIndex = event.srcElement.parentElement.parentElement.rowIndex; var styles = document.getElementById("customers"); styles.deleteRow(rowIndex); } </script></head><body> <form id="form1" runat="server"> <div> <table id="customers"> <tr> <th colspan="6" style="text-align: left;"> 添加行数:<input id="t_x1" type="text" /> <input type="button" value="添加行" onclick="javascript:AddRow();" /> <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" /></th> </tr> <tr> <th> 名字</th> <th> 管理</th> </tr> </table> </div> </form></body></html>protected void Button1_Click(object sender, EventArgs e) { int j = 1; while (j > 0) { if (Request.Form["t_b1" + j] != null) { //执行插入数据库。。省略 j++; } } }
document.getElementById("t_x1").value = rownum - 1;
}
</script>
HTML
<form id="form1" runat="server">
<div>
<table id="customers">
<tr>
<th colspan="6" style="text-align: left;">
添加行数:<input id="t_x1" type="text" runat="server"/>
<input type="button" value="添加行" onclick="javascript:AddRow();" />
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" /></th>
</tr>
<tr>
<th>
名字</th>
<th>
管理</th>
</tr>
</table>
</div>
</form>
CS
protected void Button1_Click(object sender, EventArgs e)
{
int num= Convert.ToInt32(t_x1.Value);
for (int i = 1; i <= num;i++ )
{
if (Request.Form["t_b1" +i] != null)
{
//执行插入数据库。。省略
string name = Request.Form["t_b1" +i].ToString();
string sql = "insert into test (name) values('" + name + "')";
DB.ExecuteDataSet(sql);
}
}
}
连接数据库: public static int ExecuteDataSet(string sql)
{
SqlConnection connection = new SqlConnection("server=.;user id=sa;password=sql2008;database=test");
//connection.Open();
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
try
{
SqlCommand command = new SqlCommand(sql, connection);
return command.ExecuteNonQuery();
}
catch
{
return 1;
}
finally
{
connection.Close();
connection.Dispose();
}
}
无论我添加几个,再删除几个在执行添加都是没问题的!!!