ASP.NET Tutorial/Page Lifecycle/Response
Содержание
[убрать]- 1 Adding a cache dependency in ASP.NET
- 2 Display all controls on a page
- 3 Displaying the HTTP headers collection in ASP.NET
- 4 Displaying URL information in ASP.NET
- 5 Response.AddCacheItemDependencies
- 6 Response.AddFileDependencies
- 7 Response.AddFileDependency
- 8 Response.AppendHeader
- 9 Response.AppendToLog
- 10 Response.ApplyAppPathModifier
- 11 Response.BinaryWrite
- 12 Response.BufferOutput
- 13 Response.Charset
- 14 Response.ClearContent
- 15 Response.ClearHeaders()
- 16 Response.ContentEncoding
- 17 Response.ContentType
- 18 Response.End()
- 19 Response.Expires
- 20 Response.ExpiresAbsolute
- 21 Response.IsClientConnected
- 22 Response.Output
- 23 Response.PICS
- 24 Response.RemoveOutputCacheItem
- 25 Response.StatusCode
- 26 Response.SuppressContent
- 27 Response.Write
- 28 Response.WriteFile
- 29 Submit a named parameter via POST
- 30 Use Response.Redirect to transfer to another aspx file (C#)
- 31 Use Response.Write to output data from a database
Adding a cache dependency in ASP.NET
<%@ Page Language="vb" %>
<%@ OutputCache Duration="300" VaryByParam="None" %>
<html>
<head>
<title>Adding a cache dependency in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.AddCacheItemDependencies("Key1")
myMessage.Text = DateTime.Now()
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
Cache("Key1") = "foo"
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:label id="myMessage" runat="server"/>
<asp:button id="Button1" text="Change Key 1" onClick="Button1_Click" runat="server"/>
</form>
</body>
</html>
Display all controls on a page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ControlTree" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
<title>Controls</title>
</head>
<body>
<div>
<i>This is static HTML (not a web control).</i>
</div>
<form id="Controls" runat="server">
<div>
<asp:panel id="MainPanel" runat="server" Height="112px">
<asp:Button id="Button1" runat="server" Text="Button1"/>
<asp:Button id="Button2" runat="server" Text="Button2"/>
<asp:Button id="Button3" runat="server" Text="Button3"/>
<asp:Label id="Label1" runat="server" Width="48px">
Name:</asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</asp:panel>
<asp:Button id="Button4" runat="server" Text="Button4"/>
</div>
</form>
<div>
<i>This is static HTML (not a web control).</i>
</div>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ControlTree : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
DisplayControl(Page.Controls, 0);
}
private void DisplayControl(ControlCollection controls, int depth)
{
foreach (Control control in controls)
{
Response.Write(new String("-", depth * 4) + "> ");
Response.Write(control.GetType().ToString() + " - <b>" + control.ID + "</b><br/>");
if (control.Controls != null)
{
DisplayControl(control.Controls, depth + 1);
}
}
}
}
Displaying the HTTP headers collection in ASP.NET
<html>
<head>
<title>Submit a named parameter via POST</title>
</head>
<body>
<form id="form1" action="NextPage.aspx" method="POST">
<h3>Name:</h3>
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="vb" %>
<html>
<head>
<title>Displaying the HTTP headers collection in ASP.NET</title>
</head>
<body>
<%
Select Case Request.HttpMethod
Case "POST"
Response.Write("POST requests not allowed!<br>")
Response.End
Case "HEAD"
Response.Write("HEAD requests not allowed!<br>")
Response.End
Case "GET"
"Process request
Response.Write("GET requests are allowed!<br>")
Case Else
Response.Write("Unknown request: not allowed!<br>")
Response.End
End Select
%>
</body>
</html>
Displaying URL information in ASP.NET
<html>
<head>
<title>Submit a named parameter via POST</title>
</head>
<body>
<form id="form1" action="NextPage.aspx" method="POST">
<h3>Name:</h3>
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="vb" %>
<html>
<head>
<title>Displaying URL information in ASP.NET</title>
</head>
<body>
<%
Dim myUri As Uri
myUri = Request.Url
Response.Write("Current request URL info ? <br><br>")
Response.Write("Protocol: " & myUri.Scheme & "<br>")
Response.Write("Port: " & myUri.Port & "<br>")
Response.Write("Host Name: " & myUri.Host & "<br>")
myUri = Request.UrlReferrer
If Not (myUri Is Nothing) Then
Response.Write("Referral URL info ? <br><br>")
Response.Write("Protocol: " & myUri.Scheme & "<br>")
Response.Write("Port: " & myUri.Port & "<br>")
Response.Write("Host Name: " & myUri.Host & "<br>")
Response.Write("App Path: " & myUri.AbsolutePath & "<br>")
Else
Response.Write("No referral URL info available.")
End If
%>
</body>
</html>
Response.AddCacheItemDependencies
<%@ Page Language="vb" %>
<%@ OutputCache Duration="300" VaryByParam="None" %>
<html>
<head>
<title>Adding cache dependencies in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim myArrayList As New ArrayList
myArrayList.Add("Key1")
myArrayList.Add("Key2")
Response.AddCacheItemDependencies(myArrayList)
myMessage.Text = DateTime.Now()
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
Cache("Key1") = "foo"
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
Cache("Key2") = "bar"
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:label id="myMessage" runat="server"/>
<asp:button id="Button1" text="Change Key 1" onClick="Button1_Click" runat="server"/>
<asp:button id="Button2" text="Change Key 2" onClick="Button2_Click" runat="server"/>
</form>
</body>
</html>
Response.AddFileDependencies
<%@ Page Language="vb" %>
<%@ OutputCache Duration="300" VaryByParam="None" %>
<html>
<head>
<title>Adding file dependencies in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim myArrayList As New ArrayList
myArrayList.Add(Server.MapPath("dep.txt"))
myArrayList.Add(Server.MapPath("dep1.txt"))
Response.AddFileDependencies(myArrayList)
myMessage.Text = DateTime.Now()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.AddFileDependency
<%@ Page Language="vb" %>
<%@ OutputCache Duration="300" VaryByParam="None" %>
<html>
<head>
<title>Adding a file dependency in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.AddFileDependency(Server.MapPath("dep.txt"))
myMessage.Text = DateTime.Now()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.AppendHeader
<%@ Page Language="vb" %>
<html>
<head>
<title>Adding an HTTP Header in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.AppendHeader("Content-Type", "text/xml")
myMessage.Text = Response.ContentType
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.AppendToLog
<%@ Page Language="vb" %>
<html>
<head>
<title>Adding an HTTP Header in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.AppendToLog("Hello from Page_Load!")
myMessage.Text = "myMessage written to IIS Log!"
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ApplyAppPathModifier
<%@ Page Language="vb" %>
<html>
<head>
<title>Creating Absolute URLs for cookieless sessions in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim NewPath As String
NewPath = Request.Url.Scheme & "://" & Request.Url.Host & _
Response.ApplyAppPathModifier(Request.Url.AbsolutePath)
myMessage.Text = "Modified Absolute URL = " & NewPath
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.BinaryWrite
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<html>
<head>
<title>Writing binary content in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim ConnStr As String
ConnStr = "Data Source=(local)\NetSDK;"
ConnStr &= "database=pubs;integrated security=true"
Dim mySqlConn As New SqlConnection(ConnStr)
mySqlConn.Open
Dim SQL As String = "SELECT logo FROM pub_info WHERE pub_id = "0736""
Dim mySqlCmd as New SqlCommand(SQL, mySqlConn)
mySqlCmd.rumandType = CommandType.Text
Dim Reader As SqlDataReader
Reader = mySqlCmd.ExecuteReader()
Dim ImageBytes() As Byte
While Reader.Read()
ImageBytes = Reader.Item(0)
End While
Response.ContentType = "image/bmp"
Response.BinaryWrite(ImageBytes)
Response.End()
Reader.Close()
mySqlConn.Close()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.BufferOutput
<%@ Page Language="vb" %>
<html>
<head>
<title>Buffering Output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.BufferOutput = False
Dim i As Integer
For i = 1 To 50
If (i > 10 And i < 30) Then
Response.BufferOutput = True
Else
Response.BufferOutput = False
End If
System.Threading.Thread.Sleep(500)
Response.Write(".")
myMessage.Text &= "."
"Response.Flush
Next
Response.Write("</br>Done!</br>")
myMessage.Text &= "</br>Done!</br>"
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.Charset
<%@ Page Language="vb" %>
<html>
<head>
<title>Modifying character set in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.Charset = "Windows-1255"
myMessage.Text = "Current character set is " & Response.Charset
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ClearContent
<%@ Page Language="vb" %>
<html>
<head>
<title>Clearing buffered output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.Write("This content will not be seen.")
Response.ClearContent()
myMessage.Text = "Content written with <i>Response.Write</i> was cleared."
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ClearHeaders()
<%@ Page Language="vb" %>
<html>
<head>
<title>Clearing buffered output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.AppendHeader("Content-Type", "text/xml")
Response.ClearHeaders()
myMessage.Text = Response.ContentType
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ContentEncoding
<%@ Page Language="vb" %>
<html>
<head>
<title>Displaying Encoding in ASP.NET</title>
<script runat="server">
Sub Page_Load()
myMessage.Text = "Current encoding is " & _
Response.ContentEncoding.EncodingName & "</br>"
myMessage.Text &= "Current encoding IANA name is " & _
Response.ContentEncoding.WebName & "</br>"
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ContentType
<%@ Page Language="vb" %>
<html>
<head>
<title>Response property example</title>
<script runat="server">
Sub Page_Load()
Response.ContentType = "text/xml"
myMessage.Text = "This page will be displayed as XML in " & _
"Internet Explorer 5.0 or above."
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.End()
<%@ Page Language="vb" %>
<html>
<head>
<title>Ending processing in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.Write("Hello, World!")
Response.End()
myMessage.Text = "Hello, World!"
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.Expires
<%@ Page Language="vb" %>
<html>
<head>
<title>Cacheing Output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.CacheControl = "Public"
Response.Expires = 2
myMessage.Text = Now.ToString()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.ExpiresAbsolute
<%@ Page Language="vb" %>
<html>
<head>
<title>Cacheing Output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.CacheControl = "Public"
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(30)
myMessage.Text = Now.ToString()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.IsClientConnected
<%@ Page Language="vb" %>
<html>
<head>
<title>Checking client connection status in ASP.NET</title>
<script runat="server">
Sub Page_Load()
If Response.IsClientConnected = False Then
Response.End
Else
End If
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.Output
<%@ Page Language="vb" %>
<html>
<head>
<title>Writing Output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim myWriter As System.IO.TextWriter
myWriter = Response.Output
myWriter.WriteLine("Hello, World!")
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.PICS
<%@ Page Language="vb" %>
<html>
<head>
<title>Sending a PICS label in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim PICSLabel As String
PICSLabel &= "(PICS-1.1 <http://www.java.ru> "
PICSLabel &= "labels on " & Chr(34)
PICSLabel &= "2001.08.01T06:00-0000" & Chr(34)
PICSLabel &= " until " & Chr(34)
PICSLabel &= "2002.02.28T23:59-0000" & Chr(34)
PICSLabel &= " ratings (V 1 S 2 L 3 N 1))"
Response.PICS(PICSLabel)
myMessage.Text = PICSLabel
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.RemoveOutputCacheItem
<%@ Page Language="vb" %>
<html>
<head>
<title>Removing cache items in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Cache("Key1") = "foo"
Response.RemoveOutputCacheItem(Request.Path)
If Not Cache("Key1") Is Nothing Then
myMessage.Text = Cache("Key1")
Else
myMessage.Text = "Cache items for " & Request.Path & _
" removed."
End If
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.StatusCode
<%@ Page Language="vb" %>
<html>
<head>
<title>Sending status in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.StatusCode = 542
Response.StatusDescription = "Server Error - The code is the answer."
Response.End()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.SuppressContent
<%@ Page Language="vb" %>
<html>
<head>
<title>Suppressing content in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.Write("Hello, World!")
Response.SuppressContent = True
If Response.SuppressContent Then Response.Close()
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.Write
<%@ Page Language="vb" %>
<html>
<head>
<title>Writing character output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Dim MyChars(2) As Char
Dim MyChar As Char
MyChars(0) = "A"
MyChars(1) = "B"
MyChars(2) = "C"
For Each MyChar in MyChars
Response.Write(MyChar)
Next
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Response.WriteFile
<%@ Page Language="vb" %>
<html>
<head>
<title>Writing a file to output in ASP.NET</title>
<script runat="server">
Sub Page_Load()
Response.WriteFile("dep.txt")
End Sub
</script>
</head>
<body>
<asp:label id="myMessage" runat="server"/>
</body>
</html>
Submit a named parameter via POST
<html>
<head>
<title>Submit a named parameter via POST</title>
</head>
<body>
<form id="form1" action="NextPage.aspx" method="POST">
<h3>Name:</h3>
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="vb" %>
<html>
<head>
<title>Displaying a named request parameter in ASP.NET</title>
</head>
<body>
<%
Response.Write(Request("name") & "<br><br>")
Request.SaveAs("C:\HTTPRequest.txt", True)
Dim length As Integer
length = Request.ContentLength
Response.Write("Length of request was: " & length & " bytes.")
%>
</body>
</html>
Use Response.Redirect to transfer to another aspx file (C#)
File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Cookieless1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:HyperLink ID="lnkRedirect"
runat="server"
NavigateUrl="NextPage.aspx">Link with Relative Path</asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:Button ID="cmdLink" runat="server" Text="Redirect with Relative Path" OnClick="cmdLink_Click"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button ID="cmdLinkAbsolute" runat="server" Text="Redirect with Absolute Path" OnClick="cmdLinkAbsolute_Click"></asp:Button>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Cookieless1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["test"] = "Test String";
}
protected void cmdLink_Click(object sender, EventArgs e)
{
Response.Redirect("NextPage.aspx");
}
protected void cmdLinkAbsolute_Click(object sender, EventArgs e)
{
string url = "http://" + Request.Url.Authority +
Request.Url.Segments[0] + Request.Url.Segments[1] +
"NextPage.aspx";
Response.Redirect(url);
}
}
File: NextPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NextPage.aspx.cs" Inherits="Cookieless2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblInfo"
runat="server"
Font-Bold="True"
Font-Names="Verdana"
Font-Size="Large"></asp:Label>
</div>
</form>
</body>
</html>
File: NextPage.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Cookieless2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["test"] != null)
{
lblInfo.Text = "Successfully retrieved " + (string)Session["test"];
}
else
{
lblInfo.Text = "Session information not found.";
}
}
}
Use Response.Write to output data from a database
<%@ Page Language="VB" AutoEventWireup="false"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<body>
Here comes the data!<br />
<%
Dim connect As New SqlConnection( _
"Server=mycomp;UID=sa;password=password01;database=Dummies")
connect.Open()
Dim cmd As New SqlCommand( _
"select nameid from names where first_name ="Jeff"", _
connect)
Dim id As Int32
id = cmd.ExecuteScalar()
connect.Close()
Response.Write(id)
%>
<br />How about that?<br />
</body>
</html>