INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Extensability

Showing Code to Users by Zarcom
Posted: 10 Apr 03

Today I had a question that perked my inner geek.  Turns out it was a terribly easy solution at least for now.  I plan to extend this FAQ alot.

This FAQ guides you on how you can show your users the code behind the page that they are viewing.  At the moment it displays a simple text version of the code.  Sometime in the future(hopefully the near future I plan on extending this to display a nicely formatted page that is similiar to the VS.NET display.

If you happen to move your project off of a development machine please note that in order for this to work the code files must accompany the project.  Your code file are the .aspx.vb files.  They also must stay in the same directory as the .aspx file itself or you'll see some ugly error messages.  I know I know! I should have coded in some error handling but remember this is just version 1.  I whipped it up rather quickly so no complaining.  Anyway for now simply paste the following code into your .vb file

    Public Function GrabCode(ByVal path As String) As String
        Dim reader As New StreamReader(path)
        Dim sb As New StringBuilder()

        sb.Append("<code><pre>")
        sb.Append(reader.ReadToEnd)
        sb.Append("</pre></code>")

        reader.Close()

        GrabCode = sb.ToString

        sb = Nothing
        reader = Nothing
    End Function


Then in the event that you want the code to appear you want the code to appear (page_load, or a button click).  Use this code.

        Dim myRow As TableRow
        Dim myCell As TableCell

        myRow = New TableRow()
        myCell = New TableCell()

        myCell.Text = GrabCode(Request.PhysicalPath & ".vb")

        myRow.Cells.Add(myCell)

        tblTest.Rows.Add(myRow)


Make sure that you have declared tblTest somewhere on the designview.  It's simply a server-side table object.  Of course you can put the string that "GrabCode" returns into any text holding object and it'll work.

Yes and before I forget make sure to include these Imports at the top of your page.

Imports System.Text
Imports System.IO


Like I said give me a couple days or so and I should have this as a full fledged code viewer control.

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close