CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    2

    Directing the placement of a Document.Write output

    Hi Guys, Take a look at the following code snippet:

    <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="UTF-8" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>

    <script runat="server">

    void Page_Load() {

    Response.Write("This is a test");
    }

    </script>

    </body>
    </html>

    It output the following:

    This is a test
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>



    </body>
    </html>

    (note that the text "This is a test" was inserted at the TOP of the page, rather than in the BODY)

    I know that I can add to a Control instead of using Response.Write, but the thing is I want to insert some JS/JS SRC to the HEAD tag and I don't think there is a control that I can use for that (let me know if I'm wrong).

    Is there a way that I can get Response.Write to output where it is placed (in this case between the BODY tags)?

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Directing the placement of a Document.Write output

    To write javascript to the document you can use
    Code:
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "some_key", "alert('Hello');", true);
    MSDN: The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised

    Or you can use
    Code:
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "some_key", "alert('Hello');", true);
    MSDN: The RegisterClientScriptBlock method adds a script block to the top of the rendered page.


    The last option I know, is to use a literal

    Html
    Code:
    <body>
      ...
      some text and controls
      <asp:Literal ID="Literal1" runat="server"></asp:Literal>
      some more text
    </body>
    CodeBehind
    Code:
    Literal1.Text = "<script type='text/javascript'>alert('Hello');</script>";

  3. #3
    Join Date
    Sep 2006
    Posts
    31

    Re: Directing the placement of a Document.Write output

    I Would just use
    Code:
    <&#37;="Hy There This is a Test"%>
    Or
    Code:
    <% Response.Write("testing 1 2 3"); %>
    where ever you want your text to be.

    so just place that into your body

    Greetz kristof

  4. #4
    Join Date
    Dec 2009
    Posts
    2

    Re: Directing the placement of a Document.Write output

    Thank you dannystommen, that was exactly what I needed.

    I used Literal method as that gave me the most flexibility; I could insert what I wanted anywhere in the HTML document, including the HEAD area where I needed to in this application.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured