Web front-End code:
<%@ page language= "C #" autoeventwireup= "true" codefile= "BatchAdd.aspx.cs" inherits= "Batchadd"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" action= "batchadd.aspx" >
<div>
<table id= "Tbldata" >
<tr>
<td>
Id
</td>
<td>
Title
</td>
<td>
Smallclassname
</td>
<td>
Author
</td>
<td>
UpdateTime
</td>
</tr>
<tr id= "TRow0" >
<td>
<input type= "text" id= "txtID" name= "txtID0"/>
</td>
<td>
<input type= "text" id= "Txttitle" name= "TxtTitle0"/>
</td>
<td>
<input type= "text" id= "Txtsmallclassname" name= "TXTSMALLCLASSNAME0"/>
</td>
<td>
<input type= "text" id= "Txtauthor" name= "TxtAuthor0"/>
</td>
<td>
<input type= "text" id= "Txtupdatetime" name= "TXTUPDATETIME0"/>
</td>
</tr>
</table>
<input type= "hidden" id= "Hidnum" name= "hidnum" value= "0"/>
<input type= "button" id= "Btnadd" value= "Add"/>
<input type= "Submit" id= "Btnsave" value= "Save"/>
</div>
</form>
</body>
<script src= "Js/jquery-1.4.2.min.js" type= "Text/javascript" ></script>
<script language= "javascript" type= "Text/javascript" >
$ (function () {
$ ("#btnAdd"). Click (function () {
var num = $ ("#hidNum"). Val (); //
num = parseint (num);
num++; Click Add
$ ("#hidNum"). Val (num); Re-assign Value
$ ("#tRow0"). Clone (True). attr ("id", "trow" + num). AppendTo ("#tblData"); Clone TR and re-given ID, loaded into table
$ ("#tRow" + num + "TD"). each (function () {////Cyclic clone of the new line inside the TD
$ (this). Find ("input[type= ' text ']"). Val (""); Empties data from a cloned row
Modifying related properties
$ (this). Find ("Input[name= ' txtID0"). attr ("id", "txtid" + num). attr ("name", "txtID" + num);
$ (this). Find ("Input[name= ' TxtTitle0"). attr ("id", "txttitle" + num). attr ("name", "txttitle" + num);
$ (this). Find ("Input[name= ' TXTSMALLCLASSNAME0 ')"). attr ("id", "txtsmallclassname" + num). attr ("name", " Txtsmallclassname "+ num);
$ (this). Find ("Input[name= ' TxtAuthor0"). attr ("id", "txtauthor" + num). attr ("name", "Txtauthor" + num);
$ (this). Find ("Input[name= ' TxtUpdateTime0"). attr ("id", "txtupdatetime" + num). attr ("name", "txtupdatetime" + num);
});
});
});
</script>
CS Page Code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class BatchAdd:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (!string. IsNullOrEmpty (request["Hidnum"))
{
int num = Convert.ToInt32 (request["Hidnum"]);
String ID, title, smallclassname, author, updatetime;
int rs = 0;
if (num > 0)
{
for (int i = 0; I <= num; i++)
{
id = request["txtid" + i];
title = request["Txttitle" + i];
Smallclassname = request["Txtsmallclassname" + i];
Author = request["Txtauthor" + i];
UpdateTime = request["Txtupdatetime" + i];
String sql = "INSERT into News (title,smallclassname,author,updatetime) VALUES ('" + Title + "', '" + Smallclassname + "', '" + author + "', '" + UpdateTime + "')";
dbhelper.connstring = "server=.; Database=test;uid=sa;pwd=123 ";
if (dbhelper.executesql (SQL) > 0)
rs++;
}
Response.Redirect ("manager.aspx?rs=" + RS);
}
}
}
}
Cleverly uses jquery Clone to add multiple rows of data and update the implementation code to the database