ASP.NET心得
Posted 走出一条属于自己的路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET心得相关的知识,希望对你有一定的参考价值。
在沈阳最热的一个月,get了一项新的技能。(虽然是被迫的)
Part1
asp.net 服务器控件版
前台:
<asp:DataList ID="DataList1" runat="server" >
<HeaderTemplate>
<table border="1" style=" text-align: center;">
<tr>
<td style="width: 150px; text-align: center;">设备编号</td>
<td style="width: 150px; text-align: center;">设备型号</td>
<td style="width: 150px; text-align: center;">设备名称</td>
<td style="width: 150px; text-align: center;">设备品牌</td>
<td style="width: 150px; text-align: center;">设备型号</td>
<td style="width: 150px; text-align: center;">操作</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table class="table1" border="1" style=" text-align: center;" >
<tr tabindex="1">
<%-- <td><%# Eval("id") %></td>
<td><%# Eval("mingcheng") %></td>
<td><%# Eval("leibie") %></td>
<td><%# Eval("pinpai") %></td>
<td><%# Eval("xinghao") %></td>--%>
<td style="width: 150px; text-align: center;"><%# Eval("设备编号")%></td>
<td style="width: 150px; text-align: center;"><%# Eval("设备名称")%></td>
<td style="width: 150px; text-align: center;"><%# Eval("设备类别")%></td>
<td style="width: 150px; text-align: center;"> <%# Eval("设备品牌")%></td>
<td style="width: 150px; text-align: center;"><%# Eval("设备型号")%></td>
<td style="width: 150px; text-align: center;">
<input type="button" value="查询"/>
<input type="button" value="添加"/>
<input type="button" value="编辑"/>
<input type="button" value="删除"/>
</td>
</tr>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</table>
</ItemTemplate>
</asp:DataList>
<div>
<input type="button" value="添加记录" class="ui_input_btn01" id="addBtn" />
<input type="button" value="提交" class="ui_input_btn01" id="addBtn1" />
</div>
按钮点击事件
(此事件实现的功能:点击该行,添加样式)
<script type="text/javascript">
var bol = true;
$(".table1 tr").bind("click", function () {
bol = !bol;
if (bol == false) {
$(this).addClass("active");
}
else {
$(".table1 tr").each(function () {
$(this).removeClass("active");
});
}
});
</script>
$(document).ready(function()
{
$('#addBtn').each(function(index)
{
$(this).fancybox({
'href':'add.aspx',
'width' : '50%',
'height' : '50%',
'type' : 'iframe',
'hideOnOverlayClick' : false,
//'onCleanup' : function() { window.location.href = 'device.aspx';},
'onClosed' : function() { window.location.href = 'device.aspx';}
});
});
});
后台:
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
// ds.ReadXml(Server.MapPath("/App_Data/aspte.xml"));
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 = "SELECT * FROM `device`";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
adap.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
Part2 html版
Te.aspx(初始化界面)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="te.aspx.cs" Inherits="ajaxte.te" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<input type="button" value="添加" onclick="hd()" /><br />
<%--<input type="button" value="查询" onclick="chaxun()" />--%>
查询条件:设备编号=<input type="text" id="search" />
<input type="button" value="查询" onclick="seach();"/>
<table id="TbAdd" border="1">
<thead>
<tr>
<td><input type="checkbox" /></td>
<td>设备编号 </td>
<td>设备名称</td>
<td>设备类型</td>
<td>设备品牌</td>
<td>设备型号</td>
<td>操作</td>
</tr>
</thead>
<tbody id="Tbody1"></tbody>
</table>
<script ></script>
<script>
//页面初始加载页面(加载数据库中全部数据)
$.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 < data.d.split(',').length - 5; i += 5) {
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("<tr><td> <input type='checkbox' name='xuan' onclick='xuanzhong()'/></td><td>" + bianhao + "\t</td><td>" + mingcheng + "\t</td><td>" + leixing + "\t</td><td>" + pinpai + "\t</td><td>" + xinghao + "</td><td><input type='button' id='bianji' value='编辑' onclick='updat(this);'/></td></tr>");
$("#TbAdd tbody").append("<tr><td> <input type='checkbox' name='xuan' onclick='xuanzhong()'/></td><td>" + bianhao + "\t</td><td>" + mingcheng + "\t</td><td>" + leixing + "\t</td><td>" + pinpai + "\t</td><td>" + xinghao + "</td><td><input type='button' id='bianji' value='编辑' onclick='updat(this);'/></td></tr>");
//alert(bianhao + " " + mingcheng + " " + leixing + " " + pinpai + " " + xinghao+" ");
}
function xuanzhong() {
$(function () {
//获取索引行
var a = document.getElementsByName("xuan");
var row = new Array();
for (var i = 0; i < a.length; 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 < pchildren.length; 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 < data.d.split(',').length - 5; i += 5) {
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);
}
});
}
</script>
</body>
</html>
该界面后台:
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)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="add.aspx.cs" Inherits="ajaxte.add" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
设备编号:<input type="text" id="bianhao" /><br />
设备类型:<input type="text" id="leixing"/><br />
设备名称:<input type="text" id="mingcheng"/><br />
设备品牌:<input type="text" id="shuliang"/><br />
设备型号:<input type="text" id="beizhu"/>
</div>
<div>
<%--<input type="button" id="tijiao" onclick="newadd()" value="提交" />--%>
<a href="#" id="tianjia" onclick="newadd();">添加</a>
</div>
</form>
<script ></script>
<script>
//点击添加按钮,将文本框中数据添加至数据库
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
window.opener.hd1(data.d.split(",")[0],data.d.split(",")[1],data.d.split(",")[2],data.d.split(",")[3],data.d.split(",")[4]);
},
error: function (err) {
alert("err1" + err);
}
});
}
</script>
</body>
</html>
编辑界面(Edit.aspx) 难点:url界面传值
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="ajaxte.Edit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>编辑对话框</title>
<script ></script>
<script type="text/javascript">
$(function () {
$("#bianhao").val(getQueryString("bianhao"));
$("#leixing").val(getQueryString("leixing"));
$("#mingcheng").val(getQueryString("mingcheng"));
$("#shuliang").val(getQueryString("shuliang"));
$("#beizhu").val(getQueryString("beizhu"));
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
设备编号:<input type="text" id="bianhao" /><br />
设备类型:<input type="text" id="leixing"/><br />
设备名称:<input type="text" id="mingcheng"/><br />
设备品牌:<input type="text" id="shuliang"/><br />
设备型号:<input type="text" id="beizhu"/>
</div>
<div>
<%--<input type="button" id="tijiao" onclick="newadd()" value="提交" />--%>
<a href="#" id="tianjia" onclick="newedit();">提交</a>
</div>
</form>
<script ></script>
<script type="text/javascript">
//点击添加按钮,将文本框中数据添加至数据库
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("更新成功");
self.opener.location.reload();
window.close();
},
error: function (err) {
alert("err" + err);
}
});
}
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
</script>
</body>
</html>
Part3 MVC版
见下文
以上是关于ASP.NET心得的主要内容,如果未能解决你的问题,请参考以下文章