在沈阳最热的一个月,get了一项新的技能。(虽然是被迫的)
Part1
asp.net 服务器控件版
前台:
设备编号
设备型号
设备名称
设备品牌
设备型号
操作
--%>
按钮点击事件
(此事件实现的功能:点击该行,添加样式)
//页面初始加载页面(加载数据库中全部数据)
$.ajax({
type: "POST",
url: "te.aspx/Cha_all",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
//解析data
for (var i = 0; i
hd1(data.d.split(",")[i], data.d.split(",")[i + 1], data.d.split(",")[i + 2], data.d.split(",")[i + 3], data.d.split(",")[i + 4]);
}
},
error: function (err) {
alert("err" + err);
}
});
//弹窗
function hd() {
window.open("add.aspx", "", "height:800px,width:500px");
}
//回调函数,动态添加行
function hd1(bianhao, mingcheng, leixing, pinpai, xinghao) {
// $("#TbAdd").append(" " + bianhao + "\t" + mingcheng + "\t" + leixing + "\t" + pinpai + "\t" + xinghao + "");
$("#TbAdd tbody").append(" " + bianhao + "\t" + mingcheng + "\t" + leixing + "\t" + pinpai + "\t" + xinghao + "");
//alert(bianhao + " " + mingcheng + " " + leixing + " " + pinpai + " " + xinghao+" ");
}
function xuanzhong() {
$(function () {
//获取索引行
var a = document.getElementsByName("xuan");
var row = new Array();
for (var i = 0; i
if (a[i].checked) {
//获取行
row[i] = a[i].parentElement.parentElement.rowIndex;
//a[i].deleteRow(row[i]);
$.ajax({
type: "POST",
url: "te.aspx/Delete",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify({
bianhao:a[i].parentElement.parentElement.textContent.split('\t')[0],
leixing:a[i].parentElement.parentElement.textContent.split('\t')[1],
mingcheng:a[i].parentElement.parentElement.textContent.split('\t')[2],
shuliang:a[i].parentElement.parentElement.textContent.split('\t')[3],
beizhu:a[i].parentElement.parentElement.textContent.split('\t')[4]
}),
success: function (data) {
alert("删除成功");
location.reload();
},
error: function (err) {
alert("err" + err);
}
});
}
}
});
}
//获取数据库最后一行数据
function chaxun() {
$.ajax({
type: "POST",
url: "te.aspx/Cha",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
//解析data
hd1(data.d.split(",")[0], data.d.split(",")[1], data.d.split(",")[2], data.d.split(",")[3], data.d.split(",")[4]);
//window.open.hd1(data.d.split(",")[0],data.d.split(",")[1],data.d.split(",")[2],data.d.split(",")[3],data.d.split(",")[4]);
//alert("成功"+data.d);
// window.close();
},
error: function (err) {
alert("err" + err);
}
});
}
//修改数据
function updat(element) {
$("#TbAdd tr").bind("click", function () {
var td = $(this).find("td");
//var data = td.eq(1).html();
//alert(td.eq(1).html());
//alert(td.eq(2).html());
//alert(td.eq(3).html());
//alert(td.eq(4).html());
//alert(td.eq(5).html());
var url = "Edit.aspx?bianhao=" + td.eq(1).html() + "&leixing=" + td.eq(2).html() + "&mingcheng=" + td.eq(3).html() + "&shuliang=" + td.eq(4).html() + "&beizhu=" + td.eq(5).html();
window.open(url, "", "height:800px,width:500px");
$("#TbAdd tr").unbind("click");
});
}
//查询具体条件数据
function seach() {
var opanel = document.getElementById("Tbody1");
debugger;
var pchildren = opanel.childNodes;
debugger;
for (var a = 0; a
debugger;
opanel.removeChild(pchildren[a]);
a = -1;
}
debugger;
$.ajax({
type: "POST",
url: "te.aspx/Sur",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify({
bianhao: $("#search").val()
}),
success: function (data) {
for (var i = 0; i
hd1(data.d.split(",")[i], data.d.split(",")[i + 1], data.d.split(",")[i + 2], data.d.split(",")[i + 3], data.d.split(",")[i + 4]);
}
},
error: function (err) {
alert("err" + err);
}
});
}
该界面后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using MySql.Data.MySqlClient;
using System.Data;
namespace ajaxte
{
public partial class te : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static void GetValue(string bianhao, string leixing, string mingcheng, string shuliang, string beizhu)
{
try
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "INSERT INTO device (设备编号,设备名称,设备类别,设备品牌,设备型号)VALUES('" + bianhao + "','" + leixing + "','" + mingcheng + "','" + shuliang + "','" + beizhu + "')";
mycom.ExecuteScalar();
myconn.Close();
}
catch
{
}
}
[WebMethod]
public static string Cha()
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
string aa = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "SELECT * FROM device ORDER BY ID DESC LIMIT 1";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
MySqlDataReader dataReader = mycom.ExecuteReader();
dataReader.Read();
aa += dataReader[1] + "," + dataReader[2] + "," + dataReader[3] + "," + dataReader[4] + "," + dataReader[5];
myconn.Close();
return aa;
}
[WebMethod]
public static string Cha_all ()
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
string aa = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "SELECT * FROM device";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
MySqlDataReader dataReader = mycom.ExecuteReader();
while (dataReader.Read()) {
aa += dataReader[1] + "," + dataReader[2] + "," + dataReader[3] + "," + dataReader[4] + "," + dataReader[5]+",";
}
myconn.Close();
return aa;
}
[WebMethod]
public static void Delete(string bianhao, string leixing, string mingcheng, string shuliang, string beizhu)
{
try
{
bianhao = bianhao.Trim();
leixing = leixing.Trim();
mingcheng = mingcheng.Trim();
shuliang = shuliang.Trim();
beizhu = beizhu.Trim();
MySqlConnection myconn = null;
MySqlCommand mycom = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
// mycom.CommandText = "INSERT INTO device (设备编号,设备名称,设备类别,设备品牌,设备型号)VALUES('" + bianhao + "','" + leixing + "','" + mingcheng + "','" + shuliang + "','" + beizhu + "')";
mycom.CommandText = "DELETE FROM device WHERE 设备编号 ='" + bianhao + "' AND 设备名称 ='" + leixing + "' AND 设备类别 ='" + mingcheng + "' AND 设备品牌 ='" + shuliang + "' AND 设备型号 ='" + beizhu + "'";
mycom.ExecuteScalar();
myconn.Close();
}
catch
{
}
}
[WebMethod]
public static void Edit(string ybianhao, string yleixing, string ymingcheng, string yshuliang, string ybeizhu, string bianhao, string leixing, string mingcheng, string shuliang, string beizhu)
{
try
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "UPDATE device SET 设备编号 = '"+bianhao+"',设备名称 = '"+ leixing + "',设备类别 = '"+ mingcheng + "',设备品牌 = '"+ shuliang + "' ,设备型号 = '"+ beizhu + "' WHERE 设备编号 = '"+ ybianhao + "' AND 设备名称 = '"+ yleixing + "' AND 设备类别 = '"+ ymingcheng + "' AND `设备品牌` = '"+ yshuliang + "' AND 设备型号 = '"+ ybeizhu + "'";
mycom.ExecuteScalar();
myconn.Close();
}
catch
{
}
}
[WebMethod]
public static string Sur(string bianhao)
{
try
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
string aa = null;
myconn = new MySqlConnection("SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=mysql;PORT=3306; SslMode = None;");
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "SELECT * FROM device WHERE 设备编号='"+ bianhao + "'";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
MySqlDataReader dataReader = mycom.ExecuteReader();
while (dataReader.Read())
{
aa += dataReader[1] + "," + dataReader[2] + "," + dataReader[3] + "," + dataReader[4] + "," + dataReader[5] + ",";
}
myconn.Close();
return aa;
}
catch(Exception e)
{
return e.Message;
}
}
}
}
添加功能(跳转界面add.aspx)
设备编号:
设备类型:
设备名称:
设备品牌:
设备型号:
--%>
添加
//点击添加按钮,将文本框中数据添加至数据库
function newadd() {
$.ajax({
type: "POST",
url: "te.aspx/GetValue",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify({
bianhao: $("#bianhao").val(),
leixing: $("#leixing").val(),
mingcheng: $("#mingcheng").val(),
shuliang: $("#shuliang").val(),
beizhu: $("#beizhu").val()
}),
success: function (data) {
$("#bianhao").val() = null;
$("#leixing").val() = null;
$("#mingcheng").val() = null;
$("#shuliang").val() = null;
$("#beizhu").val() = null;
window.close();
},
error: function (err) {
alert("err" + err);
}
});
chaxun();
}
//将添加至数据库最后一行的数据显示至te页面
function chaxun() {
$.ajax({
type: "POST",
url: "te.aspx/Cha",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
//解析data
},
error: function (err) {
alert("err1" + err);
}
});
}
编辑界面(Edit.aspx) 难点:url界面传值
编辑对话框
//点击添加按钮,将文本框中数据添加至数据库
function newedit() {
$.ajax({
type: "POST",
url: "te.aspx/Edit",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify({
ybianhao: getQueryString("bianhao"),
yleixing: getQueryString("leixing"),
ymingcheng: getQueryString("mingcheng"),
yshuliang: getQueryString("shuliang"),
ybeizhu:getQueryString("beizhu"),
bianhao: $("#bianhao").val(),
leixing: $("#leixing").val(),
mingcheng: $("#mingcheng").val(),
shuliang: $("#shuliang").val(),
beizhu: $("#beizhu").val()
}),
success: function (data) {
alert("更新成功");
window.close();
},
error: function (err) {
alert("err" + err);
}
});
}
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
if (r != null) return unescape(r[2]); return null;
}
Part3 MVC版
见下文
领取专属 10元无门槛券
私享最新 技术干货