9-17ASP第一节

来源:互联网 发布:计算机java二级考试 编辑:程序博客网 时间:2024/05/29 05:55

css  html  js  jquery >>>>>>>都是在浏览器中
ASP>>>>>>>>>>>>>>后台服务器中
在asp文件中的这些<asp:TextBox  ID="TextBox1....">标签
和以前我们写的<input type="text"....>其实就是一个东西
因为在执行的时候,服务器都会把<asp:TextBox...>翻译成html.因为浏览器只认识html

WebSite=====网站
WebApplication======web应用

string username=context.Request[UserName];//获取用户提交过来的
context.Response.ContentType="text/html";//响应的数据
context.Response.ContentType="";
context.Response.Weite(username);
context.Response.Weite(":你好啊");
这就是asp.net的工作原理  。
提交给那个服务端的处理程序。用form  action属性给出
提交给服务器的控件一定要有name属性,这id可以随便是什么服务器只认识


今天的例题:
例一----------------------------------------ashx里面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication2
{
    /// <summary>
    /// helloword2 的摘要说明
    /// </summary>
    public class helloword2 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string fulPath = context.Server.MapPath("helloword2.htm");//得到helloword.html的全路径
            string htmlContent= File.ReadAllText(fulPath );//得到helloword.html的内容
            context.Response.Write(htmlContent);//把helloword.html的内容的输入来、、、、
            string ispostback=  context.Request ["ispostback"];//得到表单的value值
            if (ispostback == "true")
            {
                context.Response.Write("提交进去");

            }
            else
            {
                context.Response.Write("直接进去");
            }

            //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;
            }
        }
    }
}

-------------------------------------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 ="helloword2.ashx">
<input type="hidden"name="ispostback" value =true />
姓名:<input type="text" name="UserName" /><input type ="submit" value ="提交" />

</form>
</body>
</html>


例2

------------------------ashx中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    /// <summary>
    /// helloword 的摘要说明
    /// </summary>
    public class helloword : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string username=context.Request["UserName"];//获取用户提交过来的name为UserName的表单的值
            context.Response.ContentType = "text/html";//响应的数据时html
            context.Response.Write(@"姓名:<input type='text' name='UserName'value='"+username+@"'/><input type ='submit' value ='提交' >");
           
            context.Response.Write(username);//将表单值写回到浏览器中
            context.Response.Write(":你好啊");
        }

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

----------------------------html中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    /// <summary>
    /// helloword 的摘要说明
    /// </summary>
    public class helloword : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string username=context.Request["UserName"];//获取用户提交过来的name为UserName的表单的值
            context.Response.ContentType = "text/html";//响应的数据时html
            context.Response.Write(@"姓名:<input type='text' name='UserName'value='"+username+@"'/><input type ='submit' value ='提交' >");
           
            context.Response.Write(username);//将表单值写回到浏览器中
            context.Response.Write(":你好啊");
        }

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

原创粉丝点击