Generating HTML in a web service

Discussion in 'ASP .Net' started by Lloyd Sheen, Jul 3, 2007.

  1. Lloyd Sheen

    Lloyd Sheen Guest

    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
    Lloyd Sheen, Jul 3, 2007
    #1
    1. Advertising

  2. "Lloyd Sheen" <> wrote in message
    news:...
    > 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.
    --
    John Saunders [MVP]
    John Saunders [MVP], Jul 3, 2007
    #2
    1. Advertising

  3. Lloyd Sheen

    Lloyd Sheen Guest

    "John Saunders [MVP]" <john.saunders at trizetto.com> wrote in message
    news:...
    > "Lloyd Sheen" <> wrote in message
    > news:...
    >> 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.
    > --
    > John Saunders [MVP]
    >


    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
    Lloyd Sheen, Jul 3, 2007
    #3
  4. 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



    "Lloyd Sheen" wrote:

    >
    > "John Saunders [MVP]" <john.saunders at trizetto.com> wrote in message
    > news:...
    > > "Lloyd Sheen" <> wrote in message
    > > news:...
    > >> 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.
    > > --
    > > John Saunders [MVP]
    > >

    >
    > 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
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jul 3, 2007
    #4
  5. "Lloyd Sheen" <> wrote in message
    news:%23MTZ$...
    >
    > "John Saunders [MVP]" <john.saunders at trizetto.com> wrote in message
    > news:...
    >> "Lloyd Sheen" <> wrote in message
    >> news:...
    >>> 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.
    >> --
    >> John Saunders [MVP]
    >>

    >
    > 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.
    --
    John Saunders [MVP]
    John Saunders [MVP], Jul 3, 2007
    #5
  6. Lloyd Sheen

    Lloyd Sheen Guest

    "Peter Bromberg [C# MVP]" <> wrote in
    message news:...
    > 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
    >
    >
    >
    > "Lloyd Sheen" wrote:
    >
    >>
    >> "John Saunders [MVP]" <john.saunders at trizetto.com> wrote in message
    >> news:...
    >> > "Lloyd Sheen" <> wrote in message
    >> > news:...
    >> >> 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.
    >> > --
    >> > John Saunders [MVP]
    >> >

    >>
    >> 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
    >>
    >>
    >>


    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
    Lloyd Sheen, Jul 3, 2007
    #6
  7. 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()


    "Lloyd Sheen" wrote:

    > 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
    >
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jul 4, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mygoogleac
    Replies:
    0
    Views:
    398
    mygoogleac
    Sep 30, 2004
  2. mygoogleac
    Replies:
    0
    Views:
    371
    mygoogleac
    Oct 12, 2004
  3. Jean-Francois Hamelin

    Generating the Web Service code from C# class.

    Jean-Francois Hamelin, Jun 11, 2004, in forum: ASP .Net Web Services
    Replies:
    4
    Views:
    115
    Jean-Francois Hamelin
    Jun 17, 2004
  4. Water Cooler v2

    Generating a WSDL for your Web Service

    Water Cooler v2, Feb 2, 2006, in forum: ASP .Net Web Services
    Replies:
    4
    Views:
    146
    Anand[MVP]
    Feb 2, 2006
  5. Leo Violette
    Replies:
    0
    Views:
    1,026
    Leo Violette
    Apr 17, 2009
Loading...

Share This Page