asp.net 6.aspx页面
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页面的更多相关文章
- 老古董---ASP.NET中aspx页面runat="server"
自从 mvc3 被广泛的推进生产环境后,这个runat="server" 慢慢被人遗忘了... asp.net 的 webForm 基于控件的 html 渲染过程是否还记得呢?是 ...
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
在html中:name指的是用户名称,ID指的是用户注册是系统自动分配给用户的一个序列号. name是用来提交数据的,提供给表单用,可以重复: id则针对文档操作时候用,不能重复.如:document ...
- ASP.NET用户控件操作ASPX页面
定义一个不含数据的事件处理方法 用户控件 public event EventHandler Click; protected void Button1_Click(object sender, Ev ...
- ASP.NETMVC自定义错误页面真的简单吗?
Note:文章前半部分翻译自 http://benfoster.io/blog/aspnet-mvc-custom-error-pages ,着急的可直接看总结~ 如果你在设置asp.net mvc自 ...
- aspx页面调用发送邮件验证码(结合前两篇)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...
- Jquery Ajax调用aspx页面方法
Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...
- 转:jquery向普通aspx页面发送ajax请求
本文将介绍在ASP.NET中如何方便使用Ajax,第一种当然是使用jQuery的ajax,功能强大而且操作简单方便,第二种是使用.NET封装好的ScriptManager. $.ajax向普通页面发送 ...
- Asp.Net中动态页面转静态页面
关于在Asp.Net中动态页面转静态页面的方法网上比较多.结合实际的需求,我在网上找了一些源代码,并作修改.现在把修改后的代码以及说明写一下. 一个是一个页面转换的类,该类通过静态函数Changfil ...
- HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
<转载>来自网络 一.ID是在客户端脚本里用!NAME是用于获取提交表单的某表单域信息,在form里面,如果不指定Name的话,就不会发送到服务器端,所以有name属性的控件,必须指定na ...
随机推荐
- js 给元素绑定回车事件
经常会看到登录页面输入完账户密码,回车就登录了.实现方法: JQuery方法: $("#focus")为获取id为focus的元素 $("#focus").ke ...
- Google 插件的使用
每次看英文网页或者文档的时候总会碰到不认识单词,就想能不能选中单词就可以显示翻译?于是就安装Google浏览器的翻译插件,总的来说,蛮繁琐的. 1.先安装谷歌访问助手 (1.)直接百度谷歌访问助手 ( ...
- js怎么动态加载js文件(JavaScript性能优化篇)
下面介绍一种JS代码优化的一个小技巧,通过动态加载引入js外部文件来提高网页加载速度 [基本优化] 将所有需要的<script>标签都放在</body>之前,确保脚本执行之前完 ...
- python练习-使用163邮箱发送邮件
具体代码如下> #密码等敏感信息已经用****替换 import smtplib,sys from email.mime.text import MIMEText from email.head ...
- 1.springboot启动流程
SpringBoot版本:2.1.2.RELEASE 1.maven <parent> <groupId>org.springframework.boot</groupI ...
- 在Android初次的前期学习中的十二个小例子(附案例下载)
目录(点击标题即可下载): Hello:使用Intent在两个Activity之间切换 Hello2:实现特定的鼠标点击事件功能 Hello3:点击监听事件集中处理 Hello4:登录注册界面布局 H ...
- 一百零九:CMS系统之前端根据不同权限渲染不同菜单
给用户绑定为开发者 个人信息中渲染角色和权限 {% extends 'cms/cms_base.html' %} {% block title %} 个人信息{% endblock %} {% blo ...
- Linux文档中翻页和搜索关键字
按键 进行工作空格键 向下翻一页[Page Down] 向下翻一页[Page Up] 向上翻一页[Ctrl + U] 向上翻一页[Ctrl + D] 向下翻一页/string 向下搜寻string这个 ...
- rocksdb wiki文档阅读笔记
由于是英文文档,不做笔记过一阵就忘了,现在把关键点记录到这,开发的时候使用. 具体wiki地址:https://github.com/facebook/rocksdb/wiki 1)Column Fa ...
- spring-boot集成8:集成shiro,jwt
Shrio是一个轻量级的,基于AOP 和 Servlet 过滤器的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权. JWT(JSON Web Token)是 ...