上一篇我们用一般处理程序写了(ashx)写了一遍增删改查,今天我们将用Web窗体(aspx)写一遍增删改查。我们平时写的时候到底用一般处理程序呢?还是用Web窗体呢?
简单来说web窗体(aspx)是一般处理程序(ashx)的一个复杂版本。一般处理程序需要调用一个静态的Html页面,当HTML布局特别复杂的时候,这时候一般处理程序将会写起来很麻烦,开发效率太低。于是出现了Web窗体。当我们建一个Web窗体的时候,会自动生成一个WebForm.aspx.cs这样的一个类。它相当于Web窗体Form1.aspx的父类。
我们先来欣赏一下WebForm1.aspx界面
如果在WebForm1.aspx界面写C#代码必须在<% %>里面
Inherits表示继承:WebForm.aspx.cs类,它相当于Web窗体Form1.aspx类的父类。
CodeBehind:表示后置代码要在WebForm.aspx.cs里面写。
总结一点:复杂的页面布置用web窗体,简单的用一般处理程序
一、查
第一种写法
WebForm1.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebUI2.WebForm1" %>
<!DOCTYPE html>
<%@ Import Namespace="Model" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="CSS/tableStyle.css" rel="stylesheet" />
<script type="text/javascript">
window.onload = function () {
var datas = document.getElementsByClassName("deletes");
var dataLength = datas.length;
for (var i = 0; i < dataLength; i++) {
datas[i].onclick = function () {
if (!confirm("确定要删除吗?")) {
return false;
}
}
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="Adduser.aspx">添加用户</a>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>时间</th>
<th>删除</th>
<th>详细</th>
<th>编辑</th>
</tr>
<%--<% =StrHtml %>--%>
<% foreach (userInfo user in userList)
{%>
<tr>
<td><%=user.Id %></td>
<td><%=user.userName %></td>
<td><%=user.userPass %></td>
<td><%=user.Email %></td>
<td><%=user.regTime.ToShortDateString()%></td>
<td><a href="Del.ashx?id=<%=user.Id %>"class="deletes">删除</a></td>
<td> 详细</td>
<td> <a href="UpdateUser.aspx?id=<%=user.Id %>">编辑</a></td>
</tr>
<%} %>
</table>
</div>
</form>
</body>
</html>
WebForm.aspx.cs页面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace WebUI2
{
public partial class WebForm1 : System.Web.UI.Page
{
public List<userInfo> userList { get; set; }
/// <summary>
/// 页面加载完成以后执行Load事件,aspx界面相当于父类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
userInfoBLL bll = new userInfoBLL();
//接收返回来的数据
List<userInfo> list = bll.GetList();
userList = list;
}
}
}
第二种写法:
WebForm1.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebUI2.WebForm1" %>
<!DOCTYPE html>
<%@ Import Namespace="Model" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="CSS/tableStyle.css" rel="stylesheet" />
<script type="text/javascript">
window.onload = function () {
var datas = document.getElementsByClassName("deletes");
var dataLength = datas.length;
for (var i = 0; i < dataLength; i++) {
datas[i].onclick = function () {
if (!confirm("确定要删除吗?")) {
return false;
}
}
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="Adduser.aspx">添加用户</a>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>时间</th>
<th>删除</th>
<th>详细</th>
<th>编辑</th>
</tr>
<% =StrHtml %>
</table>
</div>
</form>
</body>
</html>
WebForm.aspx.cs页面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace WebUI2
{
public partial class WebForm1 : System.Web.UI.Page
{ public string StrHtml { get; set; }
/// <summary>
/// 页面加载完成以后执行Load事件,aspx界面相当于父类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
userInfoBLL bll = new userInfoBLL();
//接收返回来的数据
List<userInfo> list = bll.GetList();
//userList = list;
StringBuilder sb = new StringBuilder();
//遍历实体信息
foreach (userInfo user in list)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><a href='DeleteUser.ashx?id={0}'class='deletes'>删除</a></tr>"
, user.Id, user.userName, user.userPass, user.Email, user.regTime.ToShortDateString());
}
//将遍历结果给StrHtml
StrHtml = sb.ToString();
}
}
}
二、增
AddUser.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUser.aspx.cs" Inherits="WebUI2.AddUser" %>
<!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" name="txtName"/><br />
密码:<input type="password" name="txtPwd"/><br />
邮箱:<input type="text" name="txtMail"/><br />
<input type="submit" value="添加" />
</div>
</form>
</body>
</html>
AddUser.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace WebUI2
{
public partial class AddUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//如果隐藏域的值不为空,表示用户单击了提交按钮,发出了POST请求
if(IsPostBack)
{
InsertUser();
}
}
protected void InsertUser()
{
userInfo user=new userInfo();
user.userName = Request.Form["txtName"];
user.userPass = Request.Form["txtPwd"];
user.Email = Request.Form["txtMail"];
user.regTime = DateTime.Now;
userInfoBLL bll = new userInfoBLL();
if(bll.AddUserInfo(user))
{
Response.Redirect("WebForm1.aspx");
}
else
{
Response.Redirect("Error.html");
}
}
}
}
删:一般处理程序(ashx)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Model;
namespace WebUI2
{
/// <summary>
/// Del 的摘要说明
/// </summary>
public class Del : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id;
if (int.TryParse(context.Request.QueryString["id"], out id))
{
userInfoBLL Bll = new userInfoBLL();
if (Bll.DeleteUserInfo(id))
{
//删除成功后返回到handerUserinfo.ashx界面,重新数据库数据
context.Response.Redirect("WebForm1.aspx");
}
else
{
context.Response.Redirect("Error.html");
}
}
else
{
context.Response.Write("参数错误");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
改:
UpdateUser.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdateUser.aspx.cs" Inherits="WebUI2.UpdateUser" %>
<!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" name="txtName"value="<%=EditUser.userName %>"/><br />
密码:<input type="password" name="txtPwd"value="<%=EditUser.userPass %>"/><br />
邮箱:<input type="text" name="txtMail"value="<%=EditUser.Email %>"/><br />
<input type="hidden" name="txtId" value="<%=EditUser.Id %>" />
<input type="hidden" name="txtRegtime" value="<%=EditUser.regTime %>" />
<input type="submit" value="修改" />
</div>
</form>
</body>
</html>
UpdateUser.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace WebUI2
{
public partial class UpdateUser : System.Web.UI.Page
{
public userInfo EditUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Update();
}
else
{
ReadyUpdate();
}
}
//对数据进行更新
protected void ReadyUpdate()
{
userInfo user = new userInfo();
user.Id = Convert.ToInt32(Request.Form["txtId"]);
user.userName = Request.Form["txtName"];
user.userPass = Request.Form["txtPwd"];
user.Email = Request.Form["txtMail"];
user.regTime = Convert.ToDateTime(Request.Form["txtRegtime"]);
userInfoBLL bll = new userInfoBLL();
if (bll.UpdateUser(user))
{
Response.Redirect("WebForm1.aspx");
}
else
{
Response.Redirect("Error.html");
}
}
//获取用户的数据
public void Update()
{
int id;
if (int.TryParse(Request.QueryString["id"], out id))
{
userInfoBLL Bll = new userInfoBLL();
userInfo user = Bll.GetUserInfo(id);
if (user!=null){
EditUser = user;
}
else
{
Response.Write("Error.html");
}
}
else
{
Response.Write("Error.html");
}
}
}
}
Error.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
window.onload =function() {
setTimeout(change,1000);
}
function change() {
var time = document.getElementById("time").innerHTML;
time = parseInt(time);
time--;
if (time < 1) {
window.Location.href = "HanderUserInfo.ashx"
}
else {
document.getElementById("time").innerHTML = time;
setTimeout(change, 1000)
}
}
</script>
</head>
<body>
服务器忙!<span style="font-size:20px;color:red "id="time">5</span>秒钟后自动跳转<a href="HanderUserInfo.ashx">用户列表</a>
</body>
</html>
效果展示:
重点学习:
例如当执行修改功能的时候,我们对同一个webForm1.aspx请求了两次,第一次将修改的人的信息赋值到文本框中,第二次,修改完用户数据以后,再次请求了webForm1.aspx。第一次请求无非获取用户信息,用的是Get请求,修改完成信息以后,我们用的是Post请求。对同一个网址我们连续访问两次,两次需要的结果是不同的,我们需要判断是什么请求,这里用到了IsPostBack。
IsPostBack:
如果Post请求该属性值为true,如果Get请求该属性值为False. 它会根据__VIEWSTATE隐藏域进行判断,如果为post请求,该隐藏域的值会提交到服务端,那么ISpostBack的属性为True。
注意:
如果用ISPostBack判断是Get请求还是Post 请求,Form标签一定是runat="server"
如果去掉form标签里面的runat="server"将不会产生__VIEWSTATE
今天的分享就先到这合理,如果本篇博客对您有所帮助,记得点赞哦!