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");
}
}
}

asp.net 6.aspx页面的更多相关文章

  1. 老古董---ASP.NET中aspx页面runat="server"

    自从 mvc3 被广泛的推进生产环境后,这个runat="server" 慢慢被人遗忘了... asp.net 的 webForm 基于控件的 html  渲染过程是否还记得呢?是 ...

  2. HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值

    在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号. name是用来提交数据的,提供给表单用,可以重复: id则针对文档操作时候用,不能重复.如:document ...

  3. ASP.NET用户控件操作ASPX页面

    定义一个不含数据的事件处理方法 用户控件 public event EventHandler Click; protected void Button1_Click(object sender, Ev ...

  4. ASP.NETMVC自定义错误页面真的简单吗?

    Note:文章前半部分翻译自 http://benfoster.io/blog/aspnet-mvc-custom-error-pages ,着急的可直接看总结~ 如果你在设置asp.net mvc自 ...

  5. aspx页面调用发送邮件验证码(结合前两篇)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...

  6. Jquery Ajax调用aspx页面方法

    Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...

  7. 转:jquery向普通aspx页面发送ajax请求

    本文将介绍在ASP.NET中如何方便使用Ajax,第一种当然是使用jQuery的ajax,功能强大而且操作简单方便,第二种是使用.NET封装好的ScriptManager. $.ajax向普通页面发送 ...

  8. Asp.Net中动态页面转静态页面

    关于在Asp.Net中动态页面转静态页面的方法网上比较多.结合实际的需求,我在网上找了一些源代码,并作修改.现在把修改后的代码以及说明写一下. 一个是一个页面转换的类,该类通过静态函数Changfil ...

  9. HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值

    <转载>来自网络 一.ID是在客户端脚本里用!NAME是用于获取提交表单的某表单域信息,在form里面,如果不指定Name的话,就不会发送到服务器端,所以有name属性的控件,必须指定na ...

随机推荐

  1. 用gcov来检查Qt C++程序的代码覆盖率

      最近才发现MinGW里面包含一个叫做gcov的工具,可以用来检查你的程序运行时调用了哪些代码,同时显示代码行被调用的次数.这个功能在代码的覆盖率和性能调优方便都能用上. 我的运行环境 Window ...

  2. Java-JPDA 概述

    JPDA:Java 平台调试架构(Java Platform Debugger Architecture) 它是 Java 虚拟机为调试和监控虚拟机专门提供的一套接口. 一.JPDA https:// ...

  3. 前端知识点回顾之重点篇——JavaScript异步机制

    JavaScript异步机制 来源:https://www.cnblogs.com/zhaodongyu/p/3922961.html JavaScript是单线程异步执行的,单线程意味着代码在任务队 ...

  4. easyUI之Accordion(分类)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. js实现动态显示时间

    思路: *得到当前时间 var date = new Date(); //格式化为本地时间 var d1 = date.toLocaleString(); *使页面每秒显示一次时间 setInterv ...

  6. centos6里面装zabbix(五)

    今天说使用ICMP ping监控server与agent端的网络状态 今天要使用的是fping,这个软件包需要去官网下载,官网地址:http://www.fping.org/.现在的最新版是4.0 第 ...

  7. 【转载】网页JS获取当前地理位置(省市区)

    眼看2014又要过去了,翻翻今年的文章好像没有写几篇,忙真的或许已经不能成为借口了,在忙时间还是有的,就像海绵里的水挤挤总会有滴.真真的原因是没有学习过什么新的技术,工作过程中遇到的问题也不是非常难并 ...

  8. CentOS 7系统配置上的变化

    http://www.linuxidc.com/Linux/2014-09/107375p4.htm CentOS 7系统配置上的变化解析 ip ss指令替代 ifconfig route arp n ...

  9. Turbine Netflix

    Turbine是聚合服务器发送事件流数据的一个工具,用来监控集群下hystrix的metrics情况. 本实例是在consul做为服务发现时的配置. Turbine聚合器: 可以参照:https:// ...

  10. Spring Boot AOP Demo

    什么是AOP? AOP面向切面,切面将那些与业务无关,却被业务模块共同调用的逻辑提取并封装起来,减少了系统中的重复代码,降低了模块间的耦合度,同时提高了系统的可维护性. 实现策略JAVA SE动态代理 ...