Generating HTML in a web service

L

Lloyd Sheen

Perhaps I have missed something but what I would like to do is have a more
"controlled" method of generating HTML from a web service.

I can create items using HtmlTable, HtmlTableRow, and HtmlTableCell but is
there a quick method once the table has been built to get the HTML, put it
in a string for return the browser call for the web service?

I notice that HtmlTable does not support InnerHtml so that is not doable.

Ideas??

Thanks
Lloyd Sheen
 
J

John Saunders [MVP]

Lloyd Sheen said:
Perhaps I have missed something but what I would like to do is have a more
"controlled" method of generating HTML from a web service.

What technique are you using now for generating HTML from a web service?

The usual way to do it is to have the web service return data, then to have
an ASP.NET page bind the data to controls which will produce the HTML.
 
L

Lloyd Sheen

John Saunders said:
What technique are you using now for generating HTML from a web service?

The usual way to do it is to have the web service return data, then to
have an ASP.NET page bind the data to controls which will produce the
HTML.

At present I am just creating strings of the output HTML. I was looking for
a structured method using dotnet objects. Something like (just typed in so
most likely has errors)

<WebMethod()> _
Public Function JustATest(ByVal Name As String) As String
Dim s As String = String.Empty

Dim tab As New HtmlTable
Dim r1 As New HtmlTableRow()
Dim c1 As New HtmlTableCell()
c1.InnerText = "Col1"
Dim c2 As New HtmlTableCell()
c2.InnerText = Name
r1.Cells.Add(c1)
r1.Cells.Add(c2)
tab.Rows.Add(r1)

dim s as string=string.empty
s=tab.??????
return s
End Function


????? would be a method name which would then issue the HTML to represent
the table and its contents.

Lloyd Sheen
 
J

John Saunders [MVP]

Lloyd Sheen said:
At present I am just creating strings of the output HTML. I was looking
for a structured method using dotnet objects. Something like (just typed
in so most likely has errors)

<WebMethod()> _
Public Function JustATest(ByVal Name As String) As String
Dim s As String = String.Empty

Dim tab As New HtmlTable
Dim r1 As New HtmlTableRow()
Dim c1 As New HtmlTableCell()
c1.InnerText = "Col1"
Dim c2 As New HtmlTableCell()
c2.InnerText = Name
r1.Cells.Add(c1)
r1.Cells.Add(c2)
tab.Rows.Add(r1)

dim s as string=string.empty
s=tab.??????
return s
End Function


????? would be a method name which would then issue the HTML to represent
the table and its contents.

Well, as I said earlier, the usual way is to separate the presentation
(HTML) from the data and business logic (the web service). That's why people
usually use ASP.NET for this purpose.
 
L

Lloyd Sheen

Peter Bromberg said:
This post shows how you can render the content of servercontrols to a
string
using the RenderControl method.

http://petesbloggerama.blogspot.com/2007/07/saving-rendered-aspnet-controls-to.html

What you describe is really kind of a non-standard approach, but if that's
what you want to do, it should help.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

Thanks Peter, your method worked great. I just put it to a memory stream to
extract the HTML. I am using AJAX to call the webservice directly and then
populate a DIV with the output without having to do things like send
viewstate and whatever. I know what needs to be sent and this method is
much faster than a postback.

Thanks again this will stay in my bag of tricks.
Lloyd Sheen
 
G

Guest

you can always make use of RenderControl method to get the html:

' first create a memory stream to hold the output
Dim ms As New System.IO.MemoryStream
' now open HTML Text Writer
Dim writer As New System.Web.UI.HtmlTextWriter(New System.IO.StreamWriter(ms))
writer.BeginRender()
myHtmlTableControl.RenderControl(writer)
writer.EndRender()
writer.Flush()
' now reposition stream to the start
ms.Position = 0
Dim sr As New System.IO.StreamReader(ms)
Dim html As String = sr.ReadToEnd()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top