home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 24 aspnet applications / aspnetapplications / responseform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-18  |  10.9 KB  |  310 lines

  1. Public Class ResponseForm
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
  5.     Protected WithEvents btnImage As System.Web.UI.WebControls.LinkButton
  6.     Protected WithEvents txtImageTitle As System.Web.UI.WebControls.TextBox
  7.     Protected WithEvents btnCookie As System.Web.UI.WebControls.LinkButton
  8.     Protected WithEvents txtCookieName As System.Web.UI.WebControls.TextBox
  9.     Protected WithEvents txtCookieValue As System.Web.UI.WebControls.TextBox
  10.     Protected WithEvents litCookieValues As System.Web.UI.WebControls.Literal
  11.     Protected WithEvents litSessionInfo As System.Web.UI.WebControls.Literal
  12.     Protected WithEvents litApplicationInfo As System.Web.UI.WebControls.Literal
  13.     Protected WithEvents chkConvert As System.Web.UI.WebControls.CheckBox
  14.     Protected WithEvents Image1 As System.Web.UI.WebControls.Image
  15.  
  16. #Region " Web Form Designer Generated Code "
  17.  
  18.     'This call is required by the Web Form Designer.
  19.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  20.  
  21.     End Sub
  22.  
  23.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  24.         'CODEGEN: This method call is required by the Web Form Designer
  25.         'Do not modify it using the code editor.
  26.         InitializeComponent()
  27.     End Sub
  28.  
  29. #End Region
  30.  
  31.     ' Depending on the value you pass to the query string,
  32.     ' this page works  to retrieve a file ,an image
  33.     ' inside the page itself, and a file.
  34.  
  35.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  36.  
  37.         If Request.QueryString("docTitle") <> "" Then
  38.             ' if the docTitle argument is used, display the requested file
  39.             ShowDoc(Request.QueryString("docTitle"))
  40.  
  41.         ElseIf Request.QueryString("imgtitle") <> "" Then
  42.             ' if the imgTitle argument is specified, dynamically create
  43.             ' an image with rotated versions of the given sentence
  44.             ShowImage(Request.QueryString("imgtitle"))
  45.         Else
  46.             ' otherwise the page is executing regularly
  47.  
  48.             ' Activate the output filter if requested
  49.             If chkConvert.Checked Then
  50.                 Response.Filter = New ConvertTagFilter(Response.Filter)
  51.             End If
  52.  
  53.             ' show how you can read a shared variable defined in Global.asax
  54.             Session("Counter") = Globals.Counter
  55.  
  56.             ' Show information about the Response object
  57.             ShowCookies()
  58.             ShowSessionInfo()
  59.             ShowApplicationInfo()
  60.         End If
  61.     End Sub
  62.  
  63.     ' send the browser a document at the specified path
  64.  
  65.     Sub ShowDoc(ByVal path As String)
  66.         ' convert to a physical path.
  67.         path = Request.MapPath(path & ".html")
  68.  
  69.         If System.IO.File.Exists(path) Then
  70.             Response.Write("Here's the document you've requested<p>")
  71.             Response.WriteFile(path)
  72.         Else
  73.             Response.Write("Sorry, no document with this name.")
  74.         End If
  75.         ' prevent the remainder of the page from executing as usual
  76.         Response.End()
  77.     End Sub
  78.  
  79.     ' send the browser an image that is created dynamically
  80.     ' by rotating the text that is passed as an argument
  81.  
  82.     Sub ShowImage(ByVal imgTitle As String)
  83.         ' Create a bitmap with given width, height, and color depth.
  84.         Dim bmp As New Bitmap(400, 200, Drawing.Imaging.PixelFormat.Format16bppRgb565)
  85.         ' Get the underlying Graphics object.
  86.         Dim gr As Graphics = Graphics.FromImage(bmp)
  87.         ' Clear its background.
  88.         gr.Clear(Color.Red)
  89.  
  90.         ' create a font
  91.         Dim fnt As New Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Point)
  92.  
  93.         ' rotate it
  94.         Dim angle As Single
  95.         For angle = 0 To 360 Step 30
  96.             ' Reset coordinate transforms.
  97.             gr.ResetTransform()
  98.             ' Translate and rotate the coordinate system.
  99.             gr.TranslateTransform(200, 100)
  100.             gr.RotateTransform(angle)
  101.             ' Draw the (rotated) string.
  102.             gr.DrawString(imgTitle, fnt, Brushes.Black, 0, 0)
  103.         Next
  104.  
  105.         ' Clear current content and set returned content type.
  106.         Response.Clear()
  107.         Response.ContentType = "image/jpeg"
  108.         ' Save to the Response.OutputStream object
  109.         bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
  110.  
  111.         ' Release resources.
  112.         fnt.Dispose()
  113.         gr.Dispose()
  114.         bmp.Dispose()
  115.  
  116.         ' prevent the remainder of the page from executing as usual
  117.         Response.End()
  118.     End Sub
  119.  
  120.     ' refresh the form with a new dynamically-generated image
  121.  
  122.     Private Sub btnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImage.Click
  123.         ' change the ImageUrl of the Image control, so that it makes
  124.         ' a request to this same page.
  125.         Image1.ImageUrl = Request.Url.AbsolutePath & "?imgtitle=" & txtImageTitle.Text
  126.     End Sub
  127.  
  128.     ' create a new cookie with given name and value
  129.  
  130.     Private Sub btnCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCookie.Click
  131.         ' Create a cookie with given name and value
  132.         Dim cookie As New HttpCookie(txtCookieName.Text, txtCookieValue.Text)
  133.         ' set its expiration date (2 weeks from now)
  134.         cookie.Expires = Now.AddDays(14)
  135.         ' send it to the client
  136.         Response.Cookies.Add(cookie)
  137.         ' redisplay the list of cookies
  138.         ShowCookies()
  139.     End Sub
  140.  
  141.     ' show all cookies
  142.  
  143.     Sub ShowCookies()
  144.         ' Display all the cookies in a Repeated control
  145.         Dim msg As String
  146.         Dim cookieName As String
  147.         For Each cookieName In Request.Cookies.AllKeys
  148.             ' Get the cookie with given name.
  149.             Dim cookie As HttpCookie = Request.Cookies(cookieName)
  150.             ' DIsplay info on this cookie.
  151.             msg &= String.Format("<b>{0}</b> = {1}<br>", cookieName, cookie.Value.ToString)
  152.         Next
  153.         litCookieValues.Text = msg
  154.     End Sub
  155.  
  156.     ' show all Session properties and variables
  157.  
  158.     Sub ShowSessionInfo()
  159.         Dim msg As String
  160.  
  161.         ' display important Session properties
  162.         msg &= "<b>SessionID</b> = " & Session.SessionID & "<br>"
  163.         msg &= "<b>LCID</b> = " & Session.LCID & "<br>"
  164.         msg &= "<b>CodePage</b> = " & Session.CodePage & "<br>"
  165.         msg &= "<b>IsCookieless</b> = " & Session.IsCookieless & "<br>"
  166.         msg &= "<b>IsNewSession</b> = " & Session.IsNewSession & "<br>"
  167.         msg &= "<b>IsReadOnly</b> = " & Session.IsReadOnly & "<br>"
  168.         msg &= "<b>Mode</b> = " & Session.Mode.ToString & "<br>"
  169.         msg &= "<b>Timeout</b> = " & Session.Timeout & "<p>"
  170.  
  171.         ' change the value of a Session variable, but only if
  172.         ' Sessions aren't in ReadOnly mode
  173.         If Not Session.IsReadOnly Then
  174.             Session.Add("color", "red")
  175.             If Session("price") Is Nothing Then
  176.                 Session.Add("price", 20)
  177.             Else
  178.                 Session("price") = CInt(Session("price")) + 10
  179.             End If
  180.         End If
  181.  
  182.         Dim key As String
  183.         For Each key In Session.Keys
  184.             msg &= String.Format("<b>{0}</b> = {1}<br>", key, Session.Item(key))
  185.         Next
  186.         msg &= "<p><b>Response.ApplyAppPathModifier(""anotherpage.aspx"")</b> = <br>" & _
  187.             Response.ApplyAppPathModifier("anotherpage.aspx") & "<p>"
  188.  
  189.         ' display the result
  190.         litSessionInfo.Text = msg
  191.     End Sub
  192.  
  193.     ' show Application variables
  194.  
  195.     Sub ShowApplicationInfo()
  196.         Dim msg As String
  197.  
  198.         'use the new Add method to create two variables.
  199.         Application.Add("quantity", "red")
  200.         Application.Add("total ", 2345)
  201.  
  202.         ' display all Application variables.
  203.         Dim key As String
  204.         For Each key In Application.Keys
  205.             msg &= String.Format("<b>{0}</b> = {1}<br>", key, Application(key))
  206.         Next
  207.         litApplicationInfo.Text = msg
  208.     End Sub
  209.  
  210. End Class
  211.  
  212. ' this class works as a Response filter
  213. ' it converts all <B> tags into <STRONG>
  214.  
  215. Class ConvertTagFilter
  216.     Inherits System.IO.Stream
  217.  
  218.     ' save Stream variables 
  219.     Private m_stream As System.IO.Stream
  220.     Private m_position As Long
  221.  
  222.     ' the constructor is passed the original output stream
  223.  
  224.     Public Sub New(ByVal sink As System.IO.Stream)
  225.         m_stream = sink
  226.     End Sub
  227.  
  228.     ' The following members of Stream must be overriden.
  229.  
  230.     Public Overrides ReadOnly Property CanRead() As Boolean
  231.         Get
  232.             Return True
  233.         End Get
  234.     End Property
  235.  
  236.     Public Overrides ReadOnly Property CanSeek() As Boolean
  237.         Get
  238.             Return True
  239.         End Get
  240.     End Property
  241.  
  242.     Public Overrides ReadOnly Property CanWrite() As Boolean
  243.         Get
  244.             Return True
  245.         End Get
  246.     End Property
  247.  
  248.     Public Overrides ReadOnly Property Length() As Long
  249.         Get
  250.             Return 0
  251.         End Get
  252.     End Property
  253.  
  254.     Public Overrides Property Position() As Long
  255.         Get
  256.             Return m_position
  257.         End Get
  258.         Set(ByVal Value As Long)
  259.             m_position = Value
  260.         End Set
  261.     End Property
  262.  
  263.     Public Overrides Function Seek(ByVal offset As Long, ByVal direction As System.IO.SeekOrigin) As Long
  264.         Return m_stream.Seek(offset, direction)
  265.     End Function
  266.  
  267.     Public Overrides Sub SetLength(ByVal length As Long)
  268.         m_stream.SetLength(length)
  269.     End Sub
  270.  
  271.     Public Overrides Sub Close()
  272.         m_stream.Close()
  273.     End Sub
  274.  
  275.     Public Overrides Sub Flush()
  276.         m_stream.Flush()
  277.     End Sub
  278.  
  279.     Public Overrides Function Read(ByVal bytes() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
  280.         m_stream.Read(bytes, offset, count)
  281.     End Function
  282.  
  283.     ' Write is the method that actually does the filtering.
  284.  
  285.     Public Overrides Sub Write(ByVal bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
  286.         ' Copy the byte array into an array of chars.
  287.         Dim chars(count - 1) As Char
  288.         Dim i As Integer
  289.         For i = 0 To count - 1
  290.             chars(i) = Convert.ToChar(bytes(i + offset))
  291.         Next
  292.  
  293.         ' Create a string from the char array
  294.         Dim output As New String(chars)
  295.         ' Replace all <BR> with <P>, in case-insensitive mode.
  296.         output = Replace(output, "<b>", "<strong>", , , CompareMethod.Text)
  297.         output = Replace(output, "</b>", "</strong>", , , CompareMethod.Text)
  298.  
  299.         ' Copy the string back into an array of chars.
  300.         chars = output.ToCharArray
  301.         Dim newBytes(chars.Length - 1) As Byte
  302.         ' Copy the array of chars into an array of bytes.
  303.         For i = 0 To chars.Length - 1
  304.             newBytes(i) = Convert.ToByte(chars(i))
  305.         Next
  306.  
  307.         ' Output the bytes.
  308.         m_stream.Write(newBytes, 0, count)
  309.     End Sub
  310. End Class