.NET內置對象之Response對象

  
.NET內置對象
ASP.NET爲方便程序員的開發提供了一些內置對象,包括Response、Request、Application、Session、Server和Cookies等六種。
Response對象
Response對象是HttpResponse類的一個實例,它允許將數據作爲請求的結果發送到瀏覽器中,並提供有關響應的信息。
新建一個網站,包括兩個網頁,代碼如下:
1、Default.aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
    <title>Response對象</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="彈出窗口" /></div>
    </form>
</body>
</html>
Default.aspx.cs代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.WriteFile(@"D:/My Documents/Visual Studio 2005/WebSites/Response對象/test.txt");
        Response.Write("<br>");
        Response.Write("寫入成功");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Default2.aspx");直接轉到Default2.aspx
        //彈出窗口
        Response.Write("<script>window.open('Default2.aspx','',width='330px',height='220px');location='javascript:history.go(-1)';</script>");
    }
}
2、Default2.aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!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 runat="server">
    <title>彈出窗口</title>
</head>
<body style="width:200px;height:100px;">
    <form id="form1" runat="server">
    <div style="width:330px;height:201px">
    這是一個彈出窗口!
    </div>
    </form>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章