How to insert dynamically generated HTML into the <body> of an ASP.NET page?

Discussion in 'ASP .Net' started by Rick Spiewak, Jul 24, 2003.

  1. Rick Spiewak

    Rick Spiewak Guest

    I need to generate a "buy" button as part of an ASP.NET page - this consists
    of a small HTML form with hidden fields, conforming to the requirements of a
    merchant credit card processor. PayPal is similar.

    I'm succeeding in doing this by using Writer.Write to emit my HTML, at least
    as far as getting it to work.

    However, depending on where I put MyBase.Render(Writer), I get my HTML
    either before the header or after the end of the body of the HTML generated
    by ASP.NET. Oddly enough, both Netscape and IE seem to be happy to handle
    this. But, it looks ugly and is probably non-compliant.

    Anyone have any ideas as to how to split the difference and get my added
    HTML form into the <body>?

    TIA

    Rick Spiewak
     
    Rick Spiewak, Jul 24, 2003
    #1
    1. Advertisements

  2. Off the top of my head, I wonder if adding a Literal server control to hold the
    content would serve your purpose? It is made to sit inside the existing
    server-side <form>. Maybe I've missed something?

    Ken

    I need to generate a "buy" button as part of an ASP.NET page - this consists
    of a small HTML form with hidden fields, conforming to the requirements of a
    merchant credit card processor. PayPal is similar.

    I'm succeeding in doing this by using Writer.Write to emit my HTML, at least
    as far as getting it to work.

    However, depending on where I put MyBase.Render(Writer), I get my HTML
    either before the header or after the end of the body of the HTML generated
    by ASP.NET. Oddly enough, both Netscape and IE seem to be happy to handle
    this. But, it looks ugly and is probably non-compliant.

    Anyone have any ideas as to how to split the difference and get my added
    HTML form into the <body>?

    TIA

    Rick Spiewak
     
    Ken Cox [Microsoft MVP], Jul 24, 2003
    #2
    1. Advertisements

  3. Rick Spiewak

    bruce barker Guest

    a literal control won't work because you cannot nest forms in html. the
    easisest way is to write a function that renders the html and call from the
    page:

    <body>
    <% RenderBuyForm(); %>
    <form runat=server id=myform>
    ......
    </form>
    </body>
     
    bruce barker, Jul 25, 2003
    #3
  4. Rick Spiewak

    Rick Spiewak Guest

    Thanks for your suggestion. But, anything which creates another <form>
    tag at design time causes errors. I'm looking for an event (for example)
    to tie into which lets me emit HTML inside of the <body> tag. Or, even
    inside the <html> tags, since more than one <body> is (still?)
    technically OK.
     
    Rick Spiewak, Jul 25, 2003
    #4
  5. Rick Spiewak

    Rick Spiewak Guest

    I am successfully using Bruce's suggestion. One subtlety - the routine gets
    called extra times. I found I can avoid this by using:

    ' Prevent double-painting of screen - wait for Render event

    If Not Rendering Then Exit Function

    Rendering = False



    where "Rendering" is a boolean flag set to true in the Render override:



    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Rendering = True

    MyBase.Render(writer)

    End Sub
     
    Rick Spiewak, Aug 1, 2003
    #5
  6. Hi Rick,

    I am glad to hear that. Thank you for sharing the knowledge in the
    newsgroup.

    Best Regards,
    Lewis

    Get Secure! - www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.
    --------------------
    | Reply-To: "Rick Spiewak" <>
    | From: "Rick Spiewak" <>
    | References: <>
    <en#>
    <#>
    <#>
    | Subject: Re: How to insert dynamically generated HTML into the <body> of
    an ASP.NET page?
    | Date: Fri, 1 Aug 2003 01:27:04 -0400
    | Lines: 98
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    | Message-ID: <eEhAM2#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 209-6-243-228.c3-0.frm-ubr1.sbo-frm.ma.cable.rcn.com
    209.6.243.228
    | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:164027
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | I am successfully using Bruce's suggestion. One subtlety - the routine
    gets
    | called extra times. I found I can avoid this by using:
    |
    | ' Prevent double-painting of screen - wait for Render event
    |
    | If Not Rendering Then Exit Function
    |
    | Rendering = False
    |
    |
    |
    | where "Rendering" is a boolean flag set to true in the Render override:
    |
    |
    |
    | Protected Overrides Sub Render(ByVal writer As
    System.Web.UI.HtmlTextWriter)
    |
    | Rendering = True
    |
    | MyBase.Render(writer)
    |
    | End Sub
    |
    |
    |
    | | > What about using a protected variable in the code behind (protected
    | > mstrMyVariable as string="") then fill it by whatever value you want at
    | any
    | > time. Then somewhere in your HTML code add <% =mstrMyVariable %>(you
    may
    | > need to add quotes) . I use this and it works fine.
    | >
    | >
    | > | > > a literal control won't work because you cannot nest forms in html.
    the
    | > > easisest way is to write a function that renders the html and call
    from
    | > the
    | > > page:
    | > >
    | > > <body>
    | > > <% RenderBuyForm(); %>
    | > > <form runat=server id=myform>
    | > > .....
    | > > </form>
    | > > </body>
    | > >
    | > >
    | > >
    | > >
    message
    | > > | > > > Off the top of my head, I wonder if adding a Literal server control
    to
    | > > hold the
    | > > > content would serve your purpose? It is made to sit inside the
    | existing
    | > > > server-side <form>. Maybe I've missed something?
    | > > >
    | > > > Ken
    | > > >
    | > > > | > > > I need to generate a "buy" button as part of an ASP.NET page - this
    | > > consists
    | > > > of a small HTML form with hidden fields, conforming to the
    | requirements
    | > of
    | > > a
    | > > > merchant credit card processor. PayPal is similar.
    | > > >
    | > > > I'm succeeding in doing this by using Writer.Write to emit my HTML,
    at
    | > > least
    | > > > as far as getting it to work.
    | > > >
    | > > > However, depending on where I put MyBase.Render(Writer), I get my
    HTML
    | > > > either before the header or after the end of the body of the HTML
    | > > generated
    | > > > by ASP.NET. Oddly enough, both Netscape and IE seem to be happy to
    | > handle
    | > > > this. But, it looks ugly and is probably non-compliant.
    | > > >
    | > > > Anyone have any ideas as to how to split the difference and get my
    | added
    | > > > HTML form into the <body>?
    | > > >
    | > > > TIA
    | > > >
    | > > > Rick Spiewak
    | > > >
    | > > >
    | > > >
    | > >
    | > >
    | >
    | >
    |
    |
    |
     
    Lewis Wang [MSFT], Aug 1, 2003
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.