create dynamic html with vb.net

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I want to create a html page from within a vb.net program.
I need to put some data from my database into the html page when it is
created:

Protected Sub DocumentDetailHandler(ByVal sender As System.Object,
ByVal e As System.EventArgs)

Dim I As Integer
Dim ID As Integer
For I = 0 To ListView.SelectedItems.Count - 1
ID = ListView.SelectedItems(I).Tag
Next

Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & ID
Dim cmd As New OleDbCommand(sql, conn)
Dim dr As OleDbDataReader
conn.Open()
dr = cmd.ExecuteReader()

Do While (dr.Read())
dr.Item("filenaam")
dr.Item("hddlokatie")
dr.Item("datum_gemaakt")
dr.Item("filenaam"))
Loop
dr.Close()
conn.Close()

End Sub

This code retrieves the data from the database.
Now I need some code which puts these into a html page.

Marco
 
Co said:
I want to create a html page from within a vb.net program.

Are you doing this in an ASP.NET program or do you want to generate HTML?

If the former, then a ListView or DataGrid might suit you, or if the latter
then you should still be able to use a ListView (Web Controls) or DataGrid
(Web Controls) by using the control's .RenderControl method to generate the
HTML.

Of course, you can always do it the manual way by building up the desired
HTML as a string (using a stringbuilder).

Andrew
 
Are you doing this in an ASP.NET program or do you want to generate HTML?

If the former, then a ListView or DataGrid might suit you, or if the latter
then you should still be able to use a ListView (Web Controls) or DataGrid
(Web Controls)  by using the control's .RenderControl method to generate the
HTML.

Of course, you can always do it the manual way by building up the desired
HTML as a string (using a stringbuilder).

Andrew

Andrew,

I do want to create a simple html page.
How does that Stringbuilder work?

I now created following in my page but maybe it can be done different:

Public Sub CreatePage(ByVal HTMLTitle As String, ByVal HTMLDetails As
String(), ByVal HTMLText As String, ByVal HTMLFileName As String)
Dim strFile As String

strFile = ""
strFile = "<html>" & vbNewLine
strFile = strFile & "<head>" & vbNewLine
strFile = strFile & "<title>" & HTMLTitle & "</title>" &
vbNewLine
strFile = strFile & "</head><body>" & vbNewLine
strFile = strFile & "<STYLE TYPE='text/css'>" & vbNewLine
strFile = strFile & " .tableheader()" & vbNewLine
strFile = strFile & "{ " & vbNewLine
strFile = strFile & " color: White;" & vbNewLine
strFile = strFile & " background-color: #2c7393;" & vbNewLine
strFile = strFile & "}" & vbNewLine
strFile = strFile & "</STYLE>" & vbNewLine
strFile = strFile & "<table width='1024' border='0'
cellspacing='0' cellpadding='5'><tr>" & vbNewLine
strFile = strFile & "<td Class='tableheader'><a name='" &
HTMLTitle & "'>" & vbNewLine
strFile = strFile & "<font size='3' face='Verdana, Arial,
Helvetica, sans-serif'><strong>" & vbNewLine

strFile = strFile & "</strong></tr></td><tr><td>" & vbNewLine
strFile = strFile & HTMLText & vbNewLine
strFile = strFile & "</tr></td></table>" & vbNewLine
strFile = strFile & "</body></html>"
SaveTextToFile(strFile, "C:\" & HTMLFileName & ".html")

End Sub

Code is not finished, still have to put the data from the database in
there.

Marco
 
Are you doing this in an ASP.NET program or do you want to generate HTML?

If the former, then a ListView or DataGrid might suit you, or if the latter
then you should still be able to use a ListView (Web Controls) or DataGrid
(Web Controls)  by using the control's .RenderControl method to generate the
HTML.

Of course, you can always do it the manual way by building up the desired
HTML as a string (using a stringbuilder).

Andrew

Andrew,

this is what my page should look like in the end.
It's a lot of code.

<html>
<head>
<title>my document name.doc</title>
</head><body>
<STYLE TYPE="text/css">
..tableheader
{
font-size: 14px;
color: White;
background-color: #2c7393;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
..td {
font-size: 11px;
color: #666666;
background-color: #F0F0F0;
font-family: Verdana, Arial, Helvetica, sans-serif;
text-decoration: none;

border-bottom-style:none;
}
..tr {
font-size: 11px;
color: #666666;
background-color: #F0F0F0;
font-family: Verdana, Arial, Helvetica, sans-serif;
text-decoration: none;
border-bottom-style:none;
}
..form {
font-size: 11px;
font-family: Verdana, Arial, Helvetica, sans-serif;
margin: 0px;
color: #000000;
}
</STYLE>

<table width="1024" border="0" cellspacing="0" cellpadding="5">
<tr>
<td Class="tableheader"><a name="doc-6380323">
<strong>My document discription</strong></td>
</tr>
<table width=1024 border=1 cellpadding=4 cellspacing=0>
<tr><br>
<td Class="td" width=1010 align=left valign=top>
<table width=1010 border=0 cellpadding=1 cellspacing=0>
<tr Class="tr">
<td Class="td" width=150 colspan=1 align=left
valign=top><strong>Document Naam</strong></td>
<td Class="td" colspan=3>My document.doc</td>
</tr>
<tr>
<td Class="td" colspan=1><strong>Lokatie</strong></td>
<td Class="td" colspan=3>Complains</td>
</tr>
<tr>
<td Class="td" colspan=1><strong>Fysieke Lokatie</strong></td>
<td Class="td" colspan=3>C:Documents And Settings\Administrator
\My Documents\</td>
</tr>
<tr>
<td Class="td">----------</td>
</tr>
<tr>
<td Class="td" colspan=1><strong>Grootte</strong></td>
<td Class="td" colspan=1>23 kB</td>
<td Class="td" width=150 colspan=1><strong>Gemaakt op</strong></
td>
<td Class="td" colspan=1>18-03-2008</td>
</tr>
<tr>
<td Class="td" colspan=1><strong>Status</strong></td>
<td Class="td" >Draft</td>
<td Class="td" colspan=1><strong>Gewijzigd op</strong></td>
<td Class="td" >18-03-2008</td>
</tr>
<tr>
<td Class="td" ><strong>Auteur</strong></td>
<td Class="td" >Admin</td>
<td Class="td" ><strong>Verloopt</strong></td>
<td Class="td" >20-01-2005</td>
</tr>
</table></table><br>
<table Class="form" width=1024 border=0 cellpadding=4
cellspacing=0><tr>
<td><strong>Samenvatting</strong><br>
Geachte heer/mevrouw,
Graag wil ik mijn abonnement op Nuon.......etc etc text from document
</td>
</tr>
</table>
</body></html>

How could I use this in a smart way?

Marco
 
Co said:
Andrew,

I do want to create a simple html page.
How does that Stringbuilder work?

A StringBuilder is more efficient than concatenating strings: every time you
do S1=S1 & "text", it makes a completely new S1 from nothing whereas a
StringBuilder has a .capacity already reserved in memory. (Apparently,
strings are "immutable".) You use .Append to add to a stringbuilder and
..ToString to get the string out when you need it as a string.
I now created following in my page but maybe it can be done different:

Public Sub CreatePage(ByVal HTMLTitle As String, ByVal HTMLDetails As
String(), ByVal HTMLText As String, ByVal HTMLFileName As String)
Dim strFile As String

End Sub

Code is not finished, still have to put the data from the database in
there.

That's pretty much the way to do it for a simple page.

You could have a separate text file for the css and read it in with a
TextReader, which would make editing the css easier.

Also, for putting strings together, String.Format can make your code easier
to read.

HTH

Andrew
 
A StringBuilder is more efficient than concatenating strings: every time you
do S1=S1 & "text", it makes a completely new S1 from nothing whereas a
StringBuilder has a .capacity already reserved in memory. (Apparently,
strings are "immutable".) You use .Append to add to a stringbuilder and
.ToString to get the string out when you need it as a string.






That's pretty much the way to do it for a simple page.

You could have a separate text file for the css and read it in with a
TextReader, which would make editing the css easier.

Also, for putting strings together, String.Format can make your code easier
to read.

HTH

Andrew

Thanks Andrew,
Could you give me a little example to go on, including the use of a
seperate css file to read.

Cheers,
Marco
 
Co said:
Could you give me a little example to go on, including the use of a
seperate css file to read.

Well, you could have a function to create the <head> section of the html,
like

''''''''''''''''''''
Imports System.IO
Imports System.Text

Private Function makeHeadSection() As String
Dim sb As New StringBuilder
sb.Append("<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""
""http://www.w3.org/TR/html4/loose.dtd"">" & vbLf)
sb.Append("<html lang=""en"">" & vbLf)
sb.Append("<head>" & vbLf)
sb.Append("<meta http-equiv=""Content-Type"" content=""text/html;
charset=UTF-8"">" & vbLf)
sb.Append("<title>Test Page</title>" & vbLf)

Dim cssFile As String =
Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "my.css")

If File.Exists(cssFile) Then
sb.Append("<style type=""text/css"">" & vbLf)
sb.Append(My.Computer.FileSystem.ReadAllText(cssFile))
sb.Append("</style>" & vbLf)
End If

sb.Append("</head>")
Return sb.ToString
End Function

''''''''''''''''''''

where "my.css" is the name of the file with the CSS in.

Note that I've done it so that the css file is in the same directory as the
application - this could be bin\Release or bin\Debug when you're creating
it. Place the css file in the appropriate place.

Andrew
 
Well, you could have a function to create the <head> section of the html,
like

''''''''''''''''''''
Imports System.IO
Imports System.Text

Private Function makeHeadSection() As String
 Dim sb As New StringBuilder
 sb.Append("<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""
""http://www.w3.org/TR/html4/loose.dtd"">" & vbLf)
 sb.Append("<html lang=""en"">" & vbLf)
 sb.Append("<head>" & vbLf)
 sb.Append("<meta http-equiv=""Content-Type"" content=""text/html;
charset=UTF-8"">" & vbLf)
 sb.Append("<title>Test Page</title>" & vbLf)

 Dim cssFile As String =
Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "my.css")

 If File.Exists(cssFile) Then
  sb.Append("<style type=""text/css"">" & vbLf)
  sb.Append(My.Computer.FileSystem.ReadAllText(cssFile))
  sb.Append("</style>" & vbLf)
 End If

 sb.Append("</head>")
 Return sb.ToString
End Function

''''''''''''''''''''

where "my.css" is the name of the file with the CSS in.

Note that I've done it so that the css file is in the same directory as the
application - this could be bin\Release or bin\Debug when you're creating
it. Place the css file in the appropriate place.

Andrew

Thanks a lot man

Marco
 
Well, you could have a function to create the <head> section of the html,
like

''''''''''''''''''''
Imports System.IO
Imports System.Text

Private Function makeHeadSection() As String
 Dim sb As New StringBuilder
 sb.Append("<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""
""http://www.w3.org/TR/html4/loose.dtd"">" & vbLf)
 sb.Append("<html lang=""en"">" & vbLf)
 sb.Append("<head>" & vbLf)
 sb.Append("<meta http-equiv=""Content-Type"" content=""text/html;
charset=UTF-8"">" & vbLf)
 sb.Append("<title>Test Page</title>" & vbLf)

 Dim cssFile As String =
Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "my.css")

 If File.Exists(cssFile) Then
  sb.Append("<style type=""text/css"">" & vbLf)
  sb.Append(My.Computer.FileSystem.ReadAllText(cssFile))
  sb.Append("</style>" & vbLf)
 End If

 sb.Append("</head>")
 Return sb.ToString
End Function

''''''''''''''''''''

where "my.css" is the name of the file with the CSS in.

Note that I've done it so that the css file is in the same directory as the
application - this could be bin\Release or bin\Debug when you're creating
it. Place the css file in the appropriate place.

Andrew

Andrew,

In the end I need to save the html page, I had this code:

SaveTextToFile(strFile, "C:\" & HTMLFileName & ".html")

Can I do that with the StringBuilder as well?

Marco
 
Back
Top