ASP.Net内置对象之网页之间传参(一)

Response对象

主要运用于数据从服务器发送到浏览器,可以输出数据、页面跳转、各个网页之间传参数等操作。

以下讲解几个常用例子:

在页面中输出数据

主要通过Write WriteFile方法输出数据。

 

Response.Write("hello");//在网页上显示hello

Response.WriteFile(@"F:\\hello.txt");//在网页上打开hello.txt并显示

Response.Write("<script>alert('hello')</script>");//弹出一个框显示hello

Response.Write("<script>alert('跳转');location.href(’webForm1.aspx’)</script>");//弹出一个框显示“跳转”并且网页跳转到webForm1.aspx

 

页面跳转并传参

Response.Redirect("~/WebForm2.aspx"); //使用Redirect方法重定向,跳转到WebForm2.aspx 页面

在页面重定向URL时传参数,用“?”分隔开,比如:地址?参数&参数&参数。。。

string name=”chen”;

string sex=”boy”;

Response.Redirect("~/WebForm2.aspx?Name="+name+”&Sex=”+sex)

 

注意:需要另一个Request对象接收,才能在webForm.aspx上显示。

输出二进制图片

使用BinaryWrite显示二进制数据。

 

先添加命名空间,Using System.IO;

FileStream str=new FileStream(Server.MapPath(“pic.gif”).FileMode.Open);

//打开图片,并保存在文件流中

long FileSize=str.Length;//获取文件流长度

byte byt[] =new byte(FileSize);//定义一个数组,用于后面存字节

str.Read(byt,0,FileSize);//读取字节流,并转化为二进制,保存在byt数组中。

str.Close();

Response.BinaryWrite(byt);//读取二进制

 

另外

 

Response.Write(“<script>windows.Close();</script>”);//直接关闭窗口

Request对象

主要是检索浏览器向服务器发出的请求信息。就相当于获取上面Response从服务器发送到浏览器的消息。

获取网页见传递的参数。

比如:上面传参的

string name=”chen”;

Response.Redirect("~/WebForm2.aspx?Name="+name)

 

获取:

Response.Write(“使用Request[string key 方法]”+Request[Name]);

Response.Write(“使用Request.Params[string key 方法]”+Request.Params[Name]);

Response.Write(“使用Request.QueryString[string key 方法]”+Request.QueryString[Name]);

获取客户端浏览器的信息

主要使用RequestBrowser对象,访问HttpBrowserCapabilities属性获取当前的相关浏览器信息。

HttpBrowserCapabilities bb=Request.Browser;

Response.Write(“客户端浏览器相关信息”);

Response.Write(“类型:”+bb.Type);

Response.Write(“名称:”+bb.Browser;

Response.Write(“版本:”+bb.Version);

Response.Write(“操作平台:”+bb.Platform);

Response.Write(“是否支持框架:”+bb.Frames);

Response.Write(“是否支持表格:”+bb.Tables);

Response.Write(“是否支持Cookies”+bb.Cookies);

Response.Write(“远程客户端IP地址:”+bb.UserHostAddress);

....

Application对象

主要是共享一个程序级的先关信息,比如多个用户共享一个Application对象。

使用Application实现全局的存储和读取

由于应用程序中的所有页面都可以访问这个全局变量,所以要对其对象加上,加锁和解锁操作来保证数据的一致性。

比如:

Application.Lock();//加锁

Application[name]=”chen”;

Application.UnLock();//解锁

 

常常运用于网站访问计数器和网页聊天。

网站访问计数器:

1

添加一个全局应用程序类(Global.asax),添加后会自动出现

void Application_Start(object sender, EventArgs e)

void Session_Start(object sender, EventArgs e)

void Session_End(object sender, EventArgs e)

...事件,分别在以上三个事件添加方法

void Application_Start(object sender, EventArgs e)

{   Application[“count”]=0;//应用程序启动时运行   }

 

void Session_Start(object sender, EventArgs e)

{   Application.Lock();//加锁

    Application[“count”]=(int)Application[“count”]+1;

    Application.UnLock();//解锁   }

//会话启动时运行

 

void Session_End(object sender, EventArgs e)

{   Application.Lock();//加锁

    Application[“count”]=(int)Application[“count”]-1;

    Application.UnLock();//解锁   }

   //会话关闭时运行:

2

所设置的网页

 protected void Page_Load(object sender, EventArgs e)

 {  Label1.Text = "访问量:" + Application["count"]; }

 

网页聊天和上例相似。

posted @ 2016-05-02 09:34  安以痕_陈  阅读(250)  评论(0编辑  收藏  举报