-
December 16th, 2009, 01:12 PM
#1
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)?
-
December 17th, 2009, 03:28 AM
#2
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>";
-
December 17th, 2009, 04:05 AM
#3
Re: Directing the placement of a Document.Write output
I Would just use
Code:
<%="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
-
January 12th, 2010, 01:21 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|