generate html and show it

learnerguy

Member
Joined
Jul 28, 2013
Messages
20
Programming Experience
Beginner
Hi,
I am making a webpage program. when the user clicks preview it calls my function which should get any info in the textboxes and produce a page in real time in the web browser showing the template they chose with the info entered in the program. all it is doing is opening up the page without showing the updated info.
I must be missing something
here is my function
Function wdmakeit(ByVal wdshowit As Boolean) As String
        Try


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        'Reset all the indexes
        iCurBonusIndex = 0
        iCurFeatureIndex = 0
        iCurTestimonialIndex = 0
        issSaveToDataset()
        Dim sTemplate As String = Application.StartupPath & "\" & gsTempFile
        Dim wdonthego As String = Application.StartupPath & "\wdss.html"


        '****Open the template file and read in the data into wdfile
        Dim myFileIn As IO.StreamReader = IO.File.OpenText(sTemplate)
        Dim wdfile As String = myFileIn.ReadToEnd
        myFileIn.Close()




        '****Read through the wdfile looking for %%'s until no more are found
        Dim iStart As Integer = wdfile.IndexOf("%%")
        Dim iEnd As Integer
        Dim sField As String = ""
        Dim drr() As DataRow
        Dim dr As DataRow
        Dim sBuffer As String = ""
        Do While iStart >= 0
            iEnd = wdfile.IndexOf("%%", iStart + 2)
            sField = wdfile.Substring(iStart, iEnd - iStart).Replace("%", "")
            Select Case sField
                Case "Feature"
                    'Use a feature


                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "FeatureEND"
                    'The final spot for features.  Use them up.
                    Dim iC As Integer
                    sBuffer = ""
                    For iC = iCurFeatureIndex To myDs.Tables("Features").Rows.Count


                    Next
                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "Bonus"


                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "BonusEND"


                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "Testimonial"


                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "TestimonialEND"
                    'The final spot for testimonials.  Use them up.
                    Dim iC As Integer
                    sBuffer = ""
                    For iC = iCurTestimonialIndex To myDs.Tables("Testimonials").Rows.Count


                    Next
                    wdfile = wdfile.Substring(0, iStart) & sBuffer & wdfile.Substring(iEnd + 2)
                Case "FooterImage"
                    'background=
                    '****Once a field has been found.  Look in the dataset for the field.
                    drr = myDs.Tables("Fields").Select("FieldName='" & sField & "'")


                    If drr.GetUpperBound(0) >= 0 Then
                        dr = drr(0)
                        wdfile = wdfile.Replace("%%" & sField & "%%", "Background=" & dr.Item("FieldHTML"))
                    Else
                        '****If the field is not found then remove the token
                        wdfile = wdfile.Replace("%%" & sField & "%%", "")
                    End If
                Case Else


                    '****Once a field has been found.  Look in the dataset for the field.
                    drr = myDs.Tables("Fields").Select("FieldName='" & sField & "'")


                    If drr.GetUpperBound(0) >= 0 Then
                        dr = drr(0)
                        wdfile = wdfile.Replace("%%" & sField & "%%", dr.Item("FieldHTML"))
                    Else
                        '****If the field is not found then remove the token
                        wdfile = wdfile.Replace("%%" & sField & "%%", "")


                    End If
            End Select


            '****Update iStart
            iStart = wdfile.IndexOf("%%")


        Loop




        '****Now update the wdss.html File
        If IO.File.Exists(wdonthego) Then
            IO.File.Delete(wdonthego)
        End If


        Dim myFileOut As IO.Stream = IO.File.Open(wdonthego, IO.FileMode.CreateNew)
        Dim buffer() As Byte
        Dim encoder As New System.Text.ASCIIEncoding
        ReDim buffer(wdfile.Length - 1)
        encoder.GetBytes(wdfile, 0, wdfile.Length, buffer, 0)
        myFileOut.Write(buffer, 0, buffer.Length)
        myFileOut.Close()
        '****


        If wdshowit Then
            System.Diagnostics.Process.Start(wdonthego)
            '****
        End If




        Return wdonthego
Exit Function
End Function

Thanks for taking some time to have a look
-learnerguy
 
Last edited by a moderator:
I took a fast look on your code, and I saw no reference to textboxes whatsoever. So I couldn't follow where your data comes from and what you attach it to. Perhaps it was my fault in not being able to follow your algorithm. But the idea is straightforward to me. I would do it by preparing a "template" .html file (actually, the template could have any extension, since one won't use open it raw in a browser), then I'd load its content into a string, edit this string in order to replace each of the key expressions previously in the template for the actual data, and save the result into a new .html document, which could now be shown using a browser or even an instance of WebBrowser control inside the very application. Hope it helps you in any way. Good luck!
 
Hi VBobCat:)

Thanks for taking time to respond. So basically the program uses a file in the project folder called wdss.html
wdss.html is set up with the string tokens to be replaced. so it is supposed to update the file with the info replacing the %% tags with the info corresponding in the textboxes. if I generate the site it generates it with the info replaced because of the code under the save button, however this function is used when they hit the preview button. The trouble is I am not grabbing the info and showing it in the browser
it is just making a new instance of wdss.html showing the template chosen but no info entered which when someone wants to preview their work it is not showing:(

Hope I clarified things.
-Learnerguy
 
Ok, so I made up my own html file, named "template.html",that contains this:
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML lang=pt-br><HEAD><META charset=ISO-8859-1><TITLE>My Test Document</TITLE></HEAD>
<BODY>
<TABLE border="1"><TBODY>
<TR><TD>Info1</TD><TD>%Info1%</TD></TR>
<TR><TD>Info2</TD><TD>%Info2%</TD></TR>
<TR><TD>Info3</TD><TD>%Info3%</TD></TR>
</TBODY></TABLE>
</BODY></HTML>

And this piece of code did the stuff:
    Sub Main()
        Dim templateContent = FileIO.FileSystem.OpenTextFileReader("C:\Users\VBobCat\Desktop\template.html").ReadToEnd()
        Dim newContent = templateContent
        newContent = newContent.Replace("%Info1%", "First information")
        newContent = newContent.Replace("%Info2%", "Second information")
        newContent = newContent.Replace("%Info3%", "Third information")
        Dim newFileName = "C:\Users\VBobCat\Desktop\Test" & Now.ToBinary.ToString & ".html"
        FileIO.FileSystem.WriteAllText(newFileName, newContent, False)
    End Sub


I hope it helps you.
 
Hi VBobCat:)

O.K. so I figured it out. the code I posted is fine and works as supposed to. The problem was the template itself where I was replacing parts like header image etc it was not using the capital letters in the template e.g. template1 instead of Template1 which is why it is not updating.
fixed it!
thanks for your time and effort!
-Learnerguy
 
Back
Top