Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2007, 12:59 PM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Viewing HTML in ASP.NET 2.0

Is there a way to view HTML from an ASP.NET 2.0 Web Application?

I have heard that you can use System.Web.UI.WebControls.Panel or System.Web.UI.WebControls.Literal control to do this.

I have tried both and neither gave me the results I expected.

I want to load a control from a HTML or RTF file and see the results in the web page exactly how it appears in the HTML or RTF file.

Is this possible? Or do I have to purchase a third-party control?

 
Old September 8th, 2007, 01:03 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

HTML yes, RTF no.

You can display HTML (provided it's code that is normally placed within the body tags) using a literal:

myLiteral.Text = "<h1>Hello World</h1>"

should display a heading in the page.

For RTF you need to look at third party components.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 8th, 2007, 01:55 PM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How is this done from a HTML file?

 
Old September 8th, 2007, 02:36 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What do you mean with "from a HTML file"? Is the file containing the actual HTML you want to display, or are you trying to make this work *in* an HTML file?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 8th, 2007, 06:45 PM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The HTML file is the file containing the actual HTML I want to display.

 
Old September 9th, 2007, 02:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In that case, take a look at the System.IO.File class. It has convenient methods to read in a text file:

http://msdn2.microsoft.com/en-us/lib...e_methods.aspx

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 9th, 2007, 07:05 AM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Which one of the following methods are appropriate?

Public Function GetHtmlPageSource(ByVal vstrURL As String) As String
    Dim url As New Uri(vstrURL)
    Dim request As HttpWebRequest = HttpWebRequest.Create(url)

    request.Method = WebRequestMethods.Http.Get

    Dim response As HttpWebResponse = request.GetResponse
    Dim reader As New StreamReader(response.GetResponseStream())
    Dim tmp As String = reader.ReadToEnd
    response.Close()

    Return tmp
End Function

Public Function GetHtmlPageSource(ByVal vstrURL As String) As String
    Dim st As System.IO.Stream = Nothing
    Dim sr As System.IO.StreamReader = Nothing
    Dim strReturn As String = ""

    Try
        Dim req As WebRequest = WebRequest.Create(vstrURL)
        Dim resp As WebResponse = req.GetResponse()

        st = resp.GetResponseStream()

        sr = New System.IO.StreamReader(st)

        strReturn = sr.ReadToEnd()

        sr.Close() : st.Close()

        Return strReturn

    Catch ex As Exception

        Return ""

    End Try

End Function


 
Old September 9th, 2007, 07:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It depends on where the HTML file comes from. If it's stored on disk, you can simply use ReadAllText of the File class I referred you to instead.

Maybe you should try to describe in a bit more detail what it is you're trying to accomplish. Makes it easier to provide a correct answer.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 9th, 2007, 05:47 PM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The HTML is stored on the disk. I would like to call the method (GetHtmlPageSource). Then render the results of the method into a System.Web.UI.WebControls.Panel.

Something like

Panel1.? = GetHtmlPageSource("c:\temp\test.html")

 
Old September 9th, 2007, 07:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Have you looked at the System.IO.File class? This has all that you need.

http://msdn2.microsoft.com/en-us/lib...e_methods.aspx

You should be able to do this with one server control and one line of code. Use a literal control on the page as Imar suggested, then use the method on the file class that gets you the entire contents of the file:

<asp:literal runat="server" id="litHtml" />

litHtml.Text = System.IO.File.ReadAllText("filename")

You will need to resolve the file path to the physical path for the File class method to work properly. Use the MapPath method to translate the web application's virtual path to the physical path.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML email problem in asp.net sanjivbshinde ASP.NET 2.0 Professional 5 May 29th, 2008 06:49 AM
HTML Button in Asp.Net vallabh53 .NET Framework 1.x 0 October 17th, 2007 01:01 AM
viewing xml inside an html file Tomi XML 1 August 2nd, 2006 05:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.