This tutorial will show you how to generate static page using ASP.NET 2.0 and VB.
At first, you will need to import the namespace from System.IO. The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support. Class StringWriter which implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder. Class FileStream which exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations. Class StreamWriter which implements a TextWriter for writing characters to a stream in a particular encoding.
| Imports System.IO |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
We use the btn_Generate_Click and two customed functons fo Stream2Stream,GetFileStream to excute the task. The detailed code as following:| Protected Sub btn_Generate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Generate.Click Dim filename As String = Server.MapPath("staticHtml_1.html") End SubDim s As Stream = GetFileStream(filename) If s IsNot Nothing Then Using s End IfStream2Stream(s, Response.OutputStream) End UsingResponse.[End]() Dim sw As New StringWriter() Server.Execute("Main_Execute.aspx", sw) Dim content As String = sw.ToString() Response.Write(content) Response.Flush() Try Using fs As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write) Finally Using streamwriter As New StreamWriter(fs, Response.ContentEncoding) End Usingstreamwriter.Write(content) End UsingResponse.End() End TryPublic Shared Sub Stream2Stream(ByVal src As Stream, ByVal dst As Stream) Dim buf As Byte() = New Byte(4095) {} End SubWhile True Dim c As Integer = src.Read(buf, 0, buf.Length) End WhileIf c = 0 Then Return End Ifdst.Write(buf, 0, c) Public Function GetFileStream(ByVal filename As String) As Stream Try End Function Dim dt As DateTime = File.GetLastWriteTime(filename) Catch Dim ts As TimeSpan = dt - DateTime.Now If ts.TotalHours > 1 Then Return Nothing End IfReturn New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read) Return Nothing End Try |
The front end GenerateStaticPageVB.aspx page looks something like this:
| <div align="left" style="text-align: center"> <table> <tr> </table><td colspan="2" style="width: 402px"> </tr> </td> <tr> <td colspan="2" style="height: 26px; width: 402px;"> </tr><asp:Button ID="btn_Generate" runat="server" Text="Generate" OnClick="btn_Generate_Click" /></td> </div> |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The flow for the code behind page is as follows| Imports System.IO Partial Class _Default Inherits System.Web.UI.Page End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com End Sub' Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials Protected Sub btn_Generate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Generate.Click Dim filename As String = Server.MapPath("staticHtml_1.html") End SubDim s As Stream = GetFileStream(filename) If s IsNot Nothing Then Using s End IfStream2Stream(s, Response.OutputStream) End UsingResponse.[End]() Dim sw As New StringWriter() Server.Execute("Main_Execute.aspx", sw) Dim content As String = sw.ToString() Response.Write(content) Response.Flush() Try Using fs As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write) Finally Using streamwriter As New StreamWriter(fs, Response.ContentEncoding) End Usingstreamwriter.Write(content) End UsingResponse.End() End TryPublic Shared Sub Stream2Stream(ByVal src As Stream, ByVal dst As Stream) Dim buf As Byte() = New Byte(4095) {} End SubWhile True Dim c As Integer = src.Read(buf, 0, buf.Length) End WhileIf c = 0 Then Return End Ifdst.Write(buf, 0, c) Public Function GetFileStream(ByVal filename As String) As Stream Try End FunctionDim dt As DateTime = File.GetLastWriteTime(filename) Catch Dim ts As TimeSpan = dt - DateTime.Now If ts.TotalHours > 1 Then Return Nothing End IfReturn New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read) Return Nothing End Try |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
No comments:
Post a Comment