Cleverly uses jquery Clone to add multiple rows of data and update the implementation code to the database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.