home *** CD-ROM | disk | FTP | other *** search
- Public Class ResponseForm
- Inherits System.Web.UI.Page
-
- Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
- Protected WithEvents btnImage As System.Web.UI.WebControls.LinkButton
- Protected WithEvents txtImageTitle As System.Web.UI.WebControls.TextBox
- Protected WithEvents btnCookie As System.Web.UI.WebControls.LinkButton
- Protected WithEvents txtCookieName As System.Web.UI.WebControls.TextBox
- Protected WithEvents txtCookieValue As System.Web.UI.WebControls.TextBox
- Protected WithEvents litCookieValues As System.Web.UI.WebControls.Literal
- Protected WithEvents litSessionInfo As System.Web.UI.WebControls.Literal
- Protected WithEvents litApplicationInfo As System.Web.UI.WebControls.Literal
- Protected WithEvents chkConvert As System.Web.UI.WebControls.CheckBox
- Protected WithEvents Image1 As System.Web.UI.WebControls.Image
-
- #Region " Web Form Designer Generated Code "
-
- 'This call is required by the Web Form Designer.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
-
- End Sub
-
- Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
- 'CODEGEN: This method call is required by the Web Form Designer
- 'Do not modify it using the code editor.
- InitializeComponent()
- End Sub
-
- #End Region
-
- ' Depending on the value you pass to the query string,
- ' this page works to retrieve a file ,an image
- ' inside the page itself, and a file.
-
- Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
- If Request.QueryString("docTitle") <> "" Then
- ' if the docTitle argument is used, display the requested file
- ShowDoc(Request.QueryString("docTitle"))
-
- ElseIf Request.QueryString("imgtitle") <> "" Then
- ' if the imgTitle argument is specified, dynamically create
- ' an image with rotated versions of the given sentence
- ShowImage(Request.QueryString("imgtitle"))
- Else
- ' otherwise the page is executing regularly
-
- ' Activate the output filter if requested
- If chkConvert.Checked Then
- Response.Filter = New ConvertTagFilter(Response.Filter)
- End If
-
- ' show how you can read a shared variable defined in Global.asax
- Session("Counter") = Globals.Counter
-
- ' Show information about the Response object
- ShowCookies()
- ShowSessionInfo()
- ShowApplicationInfo()
- End If
- End Sub
-
- ' send the browser a document at the specified path
-
- Sub ShowDoc(ByVal path As String)
- ' convert to a physical path.
- path = Request.MapPath(path & ".html")
-
- If System.IO.File.Exists(path) Then
- Response.Write("Here's the document you've requested<p>")
- Response.WriteFile(path)
- Else
- Response.Write("Sorry, no document with this name.")
- End If
- ' prevent the remainder of the page from executing as usual
- Response.End()
- End Sub
-
- ' send the browser an image that is created dynamically
- ' by rotating the text that is passed as an argument
-
- Sub ShowImage(ByVal imgTitle As String)
- ' Create a bitmap with given width, height, and color depth.
- Dim bmp As New Bitmap(400, 200, Drawing.Imaging.PixelFormat.Format16bppRgb565)
- ' Get the underlying Graphics object.
- Dim gr As Graphics = Graphics.FromImage(bmp)
- ' Clear its background.
- gr.Clear(Color.Red)
-
- ' create a font
- Dim fnt As New Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Point)
-
- ' rotate it
- Dim angle As Single
- For angle = 0 To 360 Step 30
- ' Reset coordinate transforms.
- gr.ResetTransform()
- ' Translate and rotate the coordinate system.
- gr.TranslateTransform(200, 100)
- gr.RotateTransform(angle)
- ' Draw the (rotated) string.
- gr.DrawString(imgTitle, fnt, Brushes.Black, 0, 0)
- Next
-
- ' Clear current content and set returned content type.
- Response.Clear()
- Response.ContentType = "image/jpeg"
- ' Save to the Response.OutputStream object
- bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
-
- ' Release resources.
- fnt.Dispose()
- gr.Dispose()
- bmp.Dispose()
-
- ' prevent the remainder of the page from executing as usual
- Response.End()
- End Sub
-
- ' refresh the form with a new dynamically-generated image
-
- Private Sub btnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImage.Click
- ' change the ImageUrl of the Image control, so that it makes
- ' a request to this same page.
- Image1.ImageUrl = Request.Url.AbsolutePath & "?imgtitle=" & txtImageTitle.Text
- End Sub
-
- ' create a new cookie with given name and value
-
- Private Sub btnCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCookie.Click
- ' Create a cookie with given name and value
- Dim cookie As New HttpCookie(txtCookieName.Text, txtCookieValue.Text)
- ' set its expiration date (2 weeks from now)
- cookie.Expires = Now.AddDays(14)
- ' send it to the client
- Response.Cookies.Add(cookie)
- ' redisplay the list of cookies
- ShowCookies()
- End Sub
-
- ' show all cookies
-
- Sub ShowCookies()
- ' Display all the cookies in a Repeated control
- Dim msg As String
- Dim cookieName As String
- For Each cookieName In Request.Cookies.AllKeys
- ' Get the cookie with given name.
- Dim cookie As HttpCookie = Request.Cookies(cookieName)
- ' DIsplay info on this cookie.
- msg &= String.Format("<b>{0}</b> = {1}<br>", cookieName, cookie.Value.ToString)
- Next
- litCookieValues.Text = msg
- End Sub
-
- ' show all Session properties and variables
-
- Sub ShowSessionInfo()
- Dim msg As String
-
- ' display important Session properties
- msg &= "<b>SessionID</b> = " & Session.SessionID & "<br>"
- msg &= "<b>LCID</b> = " & Session.LCID & "<br>"
- msg &= "<b>CodePage</b> = " & Session.CodePage & "<br>"
- msg &= "<b>IsCookieless</b> = " & Session.IsCookieless & "<br>"
- msg &= "<b>IsNewSession</b> = " & Session.IsNewSession & "<br>"
- msg &= "<b>IsReadOnly</b> = " & Session.IsReadOnly & "<br>"
- msg &= "<b>Mode</b> = " & Session.Mode.ToString & "<br>"
- msg &= "<b>Timeout</b> = " & Session.Timeout & "<p>"
-
- ' change the value of a Session variable, but only if
- ' Sessions aren't in ReadOnly mode
- If Not Session.IsReadOnly Then
- Session.Add("color", "red")
- If Session("price") Is Nothing Then
- Session.Add("price", 20)
- Else
- Session("price") = CInt(Session("price")) + 10
- End If
- End If
-
- Dim key As String
- For Each key In Session.Keys
- msg &= String.Format("<b>{0}</b> = {1}<br>", key, Session.Item(key))
- Next
- msg &= "<p><b>Response.ApplyAppPathModifier(""anotherpage.aspx"")</b> = <br>" & _
- Response.ApplyAppPathModifier("anotherpage.aspx") & "<p>"
-
- ' display the result
- litSessionInfo.Text = msg
- End Sub
-
- ' show Application variables
-
- Sub ShowApplicationInfo()
- Dim msg As String
-
- 'use the new Add method to create two variables.
- Application.Add("quantity", "red")
- Application.Add("total ", 2345)
-
- ' display all Application variables.
- Dim key As String
- For Each key In Application.Keys
- msg &= String.Format("<b>{0}</b> = {1}<br>", key, Application(key))
- Next
- litApplicationInfo.Text = msg
- End Sub
-
- End Class
-
- ' this class works as a Response filter
- ' it converts all <B> tags into <STRONG>
-
- Class ConvertTagFilter
- Inherits System.IO.Stream
-
- ' save Stream variables
- Private m_stream As System.IO.Stream
- Private m_position As Long
-
- ' the constructor is passed the original output stream
-
- Public Sub New(ByVal sink As System.IO.Stream)
- m_stream = sink
- End Sub
-
- ' The following members of Stream must be overriden.
-
- Public Overrides ReadOnly Property CanRead() As Boolean
- Get
- Return True
- End Get
- End Property
-
- Public Overrides ReadOnly Property CanSeek() As Boolean
- Get
- Return True
- End Get
- End Property
-
- Public Overrides ReadOnly Property CanWrite() As Boolean
- Get
- Return True
- End Get
- End Property
-
- Public Overrides ReadOnly Property Length() As Long
- Get
- Return 0
- End Get
- End Property
-
- Public Overrides Property Position() As Long
- Get
- Return m_position
- End Get
- Set(ByVal Value As Long)
- m_position = Value
- End Set
- End Property
-
- Public Overrides Function Seek(ByVal offset As Long, ByVal direction As System.IO.SeekOrigin) As Long
- Return m_stream.Seek(offset, direction)
- End Function
-
- Public Overrides Sub SetLength(ByVal length As Long)
- m_stream.SetLength(length)
- End Sub
-
- Public Overrides Sub Close()
- m_stream.Close()
- End Sub
-
- Public Overrides Sub Flush()
- m_stream.Flush()
- End Sub
-
- Public Overrides Function Read(ByVal bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
- m_stream.Read(bytes, offset, count)
- End Function
-
- ' Write is the method that actually does the filtering.
-
- Public Overrides Sub Write(ByVal bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
- ' Copy the byte array into an array of chars.
- Dim chars(count - 1) As Char
- Dim i As Integer
- For i = 0 To count - 1
- chars(i) = Convert.ToChar(bytes(i + offset))
- Next
-
- ' Create a string from the char array
- Dim output As New String(chars)
- ' Replace all <BR> with <P>, in case-insensitive mode.
- output = Replace(output, "<b>", "<strong>", , , CompareMethod.Text)
- output = Replace(output, "</b>", "</strong>", , , CompareMethod.Text)
-
- ' Copy the string back into an array of chars.
- chars = output.ToCharArray
- Dim newBytes(chars.Length - 1) As Byte
- ' Copy the array of chars into an array of bytes.
- For i = 0 To chars.Length - 1
- newBytes(i) = Convert.ToByte(chars(i))
- Next
-
- ' Output the bytes.
- m_stream.Write(newBytes, 0, count)
- End Sub
- End Class