读书人

求解为什么删除后只执行一次?解决办

发布时间: 2012-02-29 16:44:10 作者: rapoo

求解,为什么删除后只执行一次?

HTML code
<!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>




C# code
protected void Button1_Click(object sender, EventArgs e)    {        int j = 1;        while (j > 0)        {            if (Request.Form["t_b1" + j] != null)            {               //执行插入数据库。。省略                j++;            }        }    }



正确:

如果 我添加行(如:3行)完毕以后录入数据直接点添加,会循环执行我插入数据库。

出错:
如果 我添加行(如:3行)点击删除其中一行 当我录入数据直接点添加,只会插入一条数据

另一条数据却没有插入,断点看了下 if (Request.Form["t_b1" + j] != null) 只会进入一次插入,另一条不执行。

是我写错了吗?请高手解答下,谢谢!!!


[解决办法]
我的方法不可能不行的,我已测试过的,我代码全贴出来你再试试!
JS<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);
var rownum = parseInt(document.getElementById("t_x1").value, 10);


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();
}
}
无论我添加几个,再删除几个在执行添加都是没问题的!!!

读书人网 >asp.net

热点推荐