HTML页和ashx页之间的关联

来源:互联网 发布:知乎注册要用手机 编辑:程序博客网 时间:2024/06/05 17:44

html页

<!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="hello.ashx">
<input type="text" name="username"  value="@value"/>
<input type="submit" value="提交" />
<input type="hidden" name="ispostback" value="true" /> <!--隐藏字段-->
@message  <!--充当占位符了。。-->
</form>
</body>
</html>

ashx页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace _2012._9._17
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {   //111111111111111111111111111111111111111111111111111111111
            string name = context.Request["username"]; //Request是取得 但要依靠html中的控件用name属性给它命名
            context.Response.ContentType = "text/html";//此处要将plain改为html;
       
            context.Response.Write(name);            //Response 响应
            context.Response.Write(":Hello World");
           
            //22222222222222222222222222222222222222222222222222222222
            string path=context.Server.MapPath("hello.htm"); //获取html页的地址
            string aaa= File.ReadAllText(path); //将html页的内容写出来直接在ashx文件中显示
            //context.Response.Write(aaa);
            string ispostback = context.Request["ispostback"];  //获取隐藏字段的值
            //if (ispostback == "true")           //根据隐藏字段的value来判断是否提交过
            //{
            //    context.Response.Write("提交进入");
            //}
            //else {
            //    context.Response.Write("直接进入");
            //}
            //33333333333333333333333333333333333333333333333333333333
            string username = context.Request["username"];
            string message = "";
            if (ispostback == "true")
            {
                context.Response.Write("提交进入");
                message = username + "hello";
               
            }
            else {
                context.Response.Write("直接进入");
                username = "";
                message = "Hello World";
            }
            aaa=aaa.Replace("@value", username);  //将占位符替换
            aaa = aaa.Replace("@message", message);
            context.Response.Write(aaa);
            //444444444444444444444444444444444444444444444444444444444

            //把hello.htm的内容输出
            //string username=  context.Request["UserName"];

            //  if(string.IsNullOrEmpty(username))   // IsNullOrEmpty是判断是否为空的方法。。
            //  {
            //      context.Response.Write("直接进入");
            //  }
            //  else
            //  {
            //      context.Response.Write("提交进入");
            //  }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

原创粉丝点击