1.aspx页面的头部

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"
Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
<%@ Import Namespace="CZBK.ItcastProject.Model" %>

**<%@ Page %>:页面本身也是一个对象,继承自Page,而<%@ Page %> 就是设置这个对象的属性用的。

**Language:C#语言

**AutoEventWireup:启动页面事件

**CodeBehind:代码后置,代码分离

**Inherits:继承。 aspx文件会生成一个(子类)类继承于 aspx.cs文件生成的(父类)类。

**<%@ Import Namespace="CZBK.ItcastProject.Model" %>:引用命名空间

2.aspx 和 aspx.cs

aspx文件会生成一个子类,aspx.cs文件会生成一个父类。aspx继承于aspx.cs

*在aspx.cs文件中生成一个公共属性,aspx文件可以访问到!

*在aspx.cs页面中使用C#代码,引用命名空间:<%@ Import Namespace="CZBK.ItcastProject.Model" %>;然后<%=  %> 编写C#代码,‘=’ 是response.write

UserInfoList.aspx.cs

using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
public partial class UserInfoList : System.Web.UI.Page
{
public string StrHtml { get; set; }
public List<UserInfo> UserList { get; set; } /// <summary>
/// 页面加载完成以后。Load事件 Form_Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{ }
}
}

UserInfoList.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"  Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
<%@ Import Namespace="CZBK.ItcastProject.Model" %>
<!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>
<link href="../Css/tableStyle.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table> <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr>
<%=StrHtml%>
<% foreach(UserInfo userInfo in UserList){%>
<tr>
<td><%=userInfo.Id %></td>
<td><%=userInfo.UserName %></td>
<td><%=userInfo.UserPass %></td>
<td><%=userInfo.Email %></td>
<td><%=userInfo.RegTime.ToShortDateString() %></td>
<td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>
<td>详细</td>
<td><a href="EditUser.aspx?id=<%=userInfo.Id %>">编辑</a> </td>
</tr> <%} %>
</table>
<hr />
</div>
</form>
</body>
</html>

3.Page_Load 方法

*页面加载完成触发(服务端的)

*aspx.cs中的 Page_Load 方法比aspx中的  window.onload = function () {} 方法要先执行!

4.aspx列表展示数据

UserInfoList.aspx:

1.引用命名空间

2.遍历父类的userInfo(list集合) 公共属性,循环生成一行tr

3.注意html代码可以结合C#代码编写在aspx中

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs"  Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %>
<%@ Import Namespace="CZBK.ItcastProject.Model" %>
<!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>
<link href="../Css/tableStyle.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table> <tr><th>编号</th><th>用户名</th><th>密码</th><th>邮箱</th><th>时间</th><th>删除</th><th>详细</th><th>编辑</th></tr>
<% foreach(UserInfo userInfo in UserList){%>
<tr>
<td><%=userInfo.Id %></td>
<td><%=userInfo.UserName %></td>
<td><%=userInfo.UserPass %></td>
<td><%=userInfo.Email %></td>
<td><%=userInfo.RegTime.ToShortDateString() %></td>
<td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>
<td>详细</td>
<td><a href="EditUser.aspx?id=<%=userInfo.Id %>">编辑</a> </td>
</tr>
<%} %>
</table>
<hr />
</div>
</form>
</body>
</html>

UserInfoList.aspx.cs

1.定义公共属性,以便子类访问

2.调用业务层的方法

using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
public partial class UserInfoList : System.Web.UI.Page
{
public string StrHtml { get; set; }
public List<UserInfo> UserList { get; set; } /// <summary>
/// 页面加载完成以后。Load事件 Form_Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
List<UserInfo> list = UserInfoService.GetList();
UserList = list;
}
}
}

5.删除数据

采用一般处理程序,因为删除不需要展示复杂的页面,这里用一般处理程序比aspx方便。

前台的删除代码

<td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">删除</a></td>

Delete.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
/// <summary>
/// Delete 的摘要说明
/// </summary>
public class Delete : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
BLL.UserInfoService UserInfoService = CZBK.ItcastProject.Common.CacheControl.Get<BLL.UserInfoService>();
int id;
if(int.TryParse(context.Request.QueryString["id"],out id))
{
if (UserInfoService.DeleteUserInfo(id))
{
context.Response.Redirect("UserInfoList.aspx");
}
else
{
context.Response.Redirect("Error.html");
}
}
else
{
context.Response.Redirect("Error.html");
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

6.添加数据

1. runat="server" 服务端控件! 会被.netframework解析成客户端的HTML代码返回给浏览器!但是会自动生成一个隐藏域,name="__ViewState"!这个ViewState用于保持状态连接,存储相关数据!

***IsPostBack:是根据__VIEWSTATE隐藏域进行判断的!

***如果不添加runat="server",则不会自动生成这个隐藏域,name="__ViewState"

2.aspx页面可以接受Get请求或者Post请求!注意:无论是Get请求还是Post请求,都会访问aspx.cs类中的Page_Load方法!!

3.aspx页面区分Get和Post请求:

a.在form表单中添加一个隐藏域,判断隐藏域的值;aspx.cs类中的Page_load方法中判断隐藏域的值,如果有值说明表单提交,是Post请求,否则为Get请求

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs"
Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %> <!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" method="post" action="AddUserInfo.aspx" >
<div>
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
邮箱:<input type="text" name="txtMail" /><br />
<input type="hidden" name="isPostBack" value="aaa" /><!--隐藏域的值用于判断Post请求-->
<input type="submit" value="添加用户" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
public partial class AddUserInfo : System.Web.UI.Page
{
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
protected void Page_Load(object sender, EventArgs e)
{
//判断 Get,Post请求
//如果隐藏域的值不为空,表示用户单击了提交按钮发出了POST请求
if(!string.IsNullOrEmpty(Request.Form["isPostBack"]))
{
//post请求
AddUserInfo_Method();
}
else
{
//get请求
}
} private void AddUserInfo_Method()
{
Model.UserInfo userinfo = new Model.UserInfo();
userinfo.UserName = Request.Form["txtName"];
userinfo.UserPass = Request.Form["txtPwd"];
userinfo.Email = Request.Form["txtMail"];
userinfo.RegTime = DateTime.Now;
if (UserInfoService.AddUserInfo(userinfo))
{
Response.Redirect("UserInfoList.aspx");
}
else
{
Response.Redirect("../Error.html");
}
}
}
}

b.使用服务端的 form表单,runat="server"!可以用IsPostBack来判断是否为Post请求还是Get请求!!!

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs"
Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %> <!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="hidden" name="isPostBack" value="aaa" /><!--隐藏域的值用于判断Post请求-->
<input type="submit" value="添加用户" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
public partial class AddUserInfo : System.Web.UI.Page
{
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
protected void Page_Load(object sender, EventArgs e)
{
//判断 Get,Post请求
//IsPostBack:如果是POST请求该属性的值为true,如果是GET请求该属性的值为false. //IsPostBack:是根据__VIEWSTATE隐藏域进行判断的,如果是POST请求那么该隐藏域的值会提交到服务端,
//那么IsPostBack属性也就为true.
//如果将form标签的runat="server"去掉,那么就不能用该属性进行判断是POST请求还是GET请求。
//因为去掉form标签的runat="server",那么就不会再产生 __VIEWSTATE隐藏域了。
if(IsPostBack)
{
AddUserInfo_Method();
}
} private void AddUserInfo_Method()
{
Model.UserInfo userinfo = new Model.UserInfo();
userinfo.UserName = Request.Form["txtName"];
userinfo.UserPass = Request.Form["txtPwd"];
userinfo.Email = Request.Form["txtMail"];
userinfo.RegTime = DateTime.Now;
if (UserInfoService.AddUserInfo(userinfo))
{
Response.Redirect("UserInfoList.aspx");
}
else
{
Response.Redirect("../Error.html");
}
}
}
}

7.更新数据

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditUser.aspx.cs"
Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.EditUser" %> <!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" method="post" action="EditUser.aspx">
<div>
用户名:<input type="text" name="txtName" value="<%=EditUserInfo.UserName %>" /><br />
密码:<input type="text" name="txtPwd" value="<%=EditUserInfo.UserPass %>" /><br />
邮箱:<input type="text" name="txtMail" value="<%=EditUserInfo.Email %>" /><br />
<input type="hidden" name="txtId" value="<%=EditUserInfo.Id %>" />
<input type="hidden" name="txtRegTime" value="<%=EditUserInfo.RegTime %>" />
<input type="hidden" name="Ispostback" value="aaa" />
<input type="submit" value="编辑用户" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo
{
public partial class EditUser : System.Web.UI.Page
{
BLL.UserInfoService userInfoservice = Common.CacheControl.Get<BLL.UserInfoService>();
public Model.UserInfo EditUserInfo { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.Form["Ispostback"]))
{
EditMethod();//post update
}
else
{
GetUserInfoMethod();//get
}
}
private void GetUserInfoMethod()
{
int id;
if (int.TryParse(Request.QueryString["id"], out id))
{
Model.UserInfo userinfo = userInfoservice.GetDeail(id);
if (userinfo != null)
{
EditUserInfo = userinfo;
}
else
Response.Redirect("../Error.html");
}
}
private void EditMethod()
{
Model.UserInfo userinfo = new Model.UserInfo();
userinfo.UserName = Request.Form["txtName"];
userinfo.UserPass = Request.Form["txtPwd"];
userinfo.Email = Request.Form["txtMail"];
userinfo.Id = Convert.ToInt32(Request.Form["txtId"]);
userinfo.RegTime = DateTime.Now;
if (userInfoservice.UpdateUserInfo(userinfo))
{
Response.Redirect("UserInfoList.aspx");
}
else
Response.Redirect("../Error.html");
}
}
}

最新文章

  1. zlib压缩一个文件为gzip格式
  2. MVC 好记星不如烂笔头之 ---&gt; 全局异常捕获以及ACTION捕获
  3. 被滥用的for in循环
  4. zTree的功能解析
  5. 作业八—Alpha阶段项目总结
  6. 请问如何查看mysql 的端口号?
  7. 浅谈__declspec(dllexport)和__declspec(dllimport)
  8. 引入代码后,在@override报错
  9. python 定义函数
  10. 读取spring配置文件的方法(spring读取资源文件)
  11. 如何统一修改 Altium Designer 中的字符大小
  12. Win8/Win8.1都有哪些版本?我该选择哪个?(二)
  13. UIScrollView的大全
  14. Oracle学习(十):视图,索引,序列号,同义词
  15. 关于委托:异常{ 无法将 匿名方法 转换为类型&ldquo;System.Delegate&rdquo;,因为它不是委托类型 }
  16. 并行编程之PLINQ
  17. [ios2]Emoji表情符号兼容方案 【转】
  18. Vue API(directives) 自定义指令
  19. camstart API 服务器负载均衡
  20. 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!

热门文章

  1. js的dom操作(整理)(转)
  2. hadoop用户和权限
  3. koa 项目打包(使用webpack打包koa2 框架app)
  4. 性能分析 | MySQL 的慢查分析实例
  5. 【SR汇总】算法时间效率
  6. OpenCL Workshop 1 —— 数字音频滤波
  7. python之scrapy的debug、shell、settings、pipelines
  8. java.lang.reflect.Method.getAnnotation()方法示例
  9. vue+npm+Element插件+路由
  10. dataset的find查找功能使用