ASP.Net学习笔记004--基于ashx方式的ASP.Net开发1
2016-01-11 00:20
756 查看
以前写的课程都没有附上源码,很抱歉!
课程中的源码可以加qq索要:1606841559
技术交流qq1群:251572072
技术交流qq2群:170933152
也可以自己下载:
ASP.Net学习笔记004基于hx方式的ASP.Net开发1.zip
http://credream.7958.com/down_20144363.html
用例子说明
ashx和aspx是处理前台提交数据的两种方式:
ashx:
新建/WebSite1
hello1.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Hello1.ashx">
姓名:<input type="text" value="UserName" name="UserName"/>
<input type="submit" value="提交" />
<!--
服务器只认name属性,而且name属性如果重复,会只提交第一个
id是给dom用的
-->
</form>
</body>
</html>
---------------------------------------------------------------------------
Hello1.ashx
<%@ WebHandler Language="C#" Class="Hello1" %>
using System;
using System.Web;
public class Hello1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// context.Response.ContentType = "text/plain";//返回的数据是txt格式的
//这里写plain可能导致,浏览器识别成xml
context.Response.ContentType = "text/html";//表示返回的数据是html
//把原来的html写到浏览器:
string username = context.Request["UserName"];//取得html端,传回的name为UserName的值
context.Response.Write(@"<form action='Hello1.ashx'>
姓名:<input type='text' value='"+username+@"' name='UserName'/>
<input type='submit' value='提交' />
</form>");//把原来的数据写到控件中
//在C#中加一个@就表示多行文本
context.Response.Write("Hello World");
context.Response.Write(username );//写回浏览器
}
public bool IsReusable {
get {
return false;
}
}
}
---------------------------------------------------------------------------
Hello2.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Hello2.ashx">
姓名:<input type="text" value="UserName" name="UserName"/>
<input type="submit" value="提交" />
<!--
服务器只认name属性,而且name属性如果重复,会只提交第一个
id是给dom用的
-->
</form>
</body>
</html>
---------------------------------------------------------------------------
Hello2.ashx
<%@ WebHandler Language="C#" Class="Hello2" %>
using System;
using System.Web;
public class Hello2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string fullPath = context.Server.MapPath("Hello2.htm");
string content = System.IO.File.ReadAllText(fullPath );
//直接访问这个文件也会被调用
context.Response.Write(content);
string username=context .Request ["UserName"];
if (string.IsNullOrEmpty (username )){
context.Response.Write("直接进入");
}
else
{
context.Response.Write("提交进入");
}
// context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}
---------------------------------------------------------------------------
课程中的源码可以加qq索要:1606841559
技术交流qq1群:251572072
技术交流qq2群:170933152
也可以自己下载:
ASP.Net学习笔记004基于hx方式的ASP.Net开发1.zip
http://credream.7958.com/down_20144363.html
用例子说明
ashx和aspx是处理前台提交数据的两种方式:
ashx:
新建/WebSite1
hello1.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Hello1.ashx">
姓名:<input type="text" value="UserName" name="UserName"/>
<input type="submit" value="提交" />
<!--
服务器只认name属性,而且name属性如果重复,会只提交第一个
id是给dom用的
-->
</form>
</body>
</html>
---------------------------------------------------------------------------
Hello1.ashx
<%@ WebHandler Language="C#" Class="Hello1" %>
using System;
using System.Web;
public class Hello1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// context.Response.ContentType = "text/plain";//返回的数据是txt格式的
//这里写plain可能导致,浏览器识别成xml
context.Response.ContentType = "text/html";//表示返回的数据是html
//把原来的html写到浏览器:
string username = context.Request["UserName"];//取得html端,传回的name为UserName的值
context.Response.Write(@"<form action='Hello1.ashx'>
姓名:<input type='text' value='"+username+@"' name='UserName'/>
<input type='submit' value='提交' />
</form>");//把原来的数据写到控件中
//在C#中加一个@就表示多行文本
context.Response.Write("Hello World");
context.Response.Write(username );//写回浏览器
}
public bool IsReusable {
get {
return false;
}
}
}
---------------------------------------------------------------------------
Hello2.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="Hello2.ashx">
姓名:<input type="text" value="UserName" name="UserName"/>
<input type="submit" value="提交" />
<!--
服务器只认name属性,而且name属性如果重复,会只提交第一个
id是给dom用的
-->
</form>
</body>
</html>
---------------------------------------------------------------------------
Hello2.ashx
<%@ WebHandler Language="C#" Class="Hello2" %>
using System;
using System.Web;
public class Hello2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string fullPath = context.Server.MapPath("Hello2.htm");
string content = System.IO.File.ReadAllText(fullPath );
//直接访问这个文件也会被调用
context.Response.Write(content);
string username=context .Request ["UserName"];
if (string.IsNullOrEmpty (username )){
context.Response.Write("直接进入");
}
else
{
context.Response.Write("提交进入");
}
// context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}
---------------------------------------------------------------------------
相关文章推荐
- ASP.Net学习笔记002--ASP.Net服务端控件做了什么2
- ASP.Net学习笔记001--ASP.Net简介1
- ASP 中 Cookies 的 Expires 属性的设置(JS版本)
- [Asp.net 5] Options-配置文件(2)
- 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
- ASP.NET5配置
- HTML+CSS+Asp.net 打造高大上UI
- (已测)【ASP.NET 基础】图片上传和显示
- ASP.Net请求处理机制初步探索之旅 - Part 3 管道
- 【ASP.NET】母版页与include
- Raspberry - MAC下烧录image的方法
- Raspberry - First Step
- 解决 VS2013 ASP.NET 缺少 SQL Server Express
- asp.net页面回传与js调用服务端事件、PostBack的原理详解
- ASP.NET服务器控件的生命周期分析
- ASP.NET服务器端控件原理分析
- asp.net控件本质
- 浅谈ASP.NET的Postback
- Asp.net控件之异同:HTML控件与Web服务器控件
- asp.net服务器控件与html服务器控件的区别