您的位置:首页 > 编程语言 > ASP

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;

}

}

}

---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: