Response.write

G

Guest

Hi,
just wondering about the following. if you put this statement in the page
load event of the code behind file.

Response.Write("Hello Asp.net");

Then run the page, then use the view source in the browser. the "Hello
Asp.net" is printed before the HTML and Body of the page. Please let me know
why this is.
Thanks...
 
M

Mark Fitzpatrick

Primarily because this isn't ASP and ASP.Net behaves very differently and
uses an event-driven model. A Response.Write dumps out the string during
certain events that occur during the page's execution. Basically, it's
sending all the HTML code last and the response.write will be sent earlier
because of the event it occurs in, usually the Page_Load event.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
G

Guest

oz said:
if you put this statement in the page
load event of the code behind file.

Response.Write("Hello Asp.net");

Then run the page, then use the view source in the browser. the "Hello
Asp.net" is printed before the HTML and Body of the page. Please let me know
why this is.
This is because you write it to the response stream before any other
output has been written to it. ASP.NET emits the HTML code for the page
when the page's Render method has been called. This is the last event in
the life cycle before the page is disposed.

To write "Hello ASP.NET" within the body I suggest you either place a
Label control in the body and set the Text property of this control or
use the Response.Write method within the BODY element of the HTML code.

<BODY>
<% Response.Write("Hello ASP.NET"); %>
</BODY>

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
G

Guest

there is no render, I put the code in the prerender event, with the same
problem, just out of curious....this is taking me into the birth of the page
:)......
 
G

Guest

oz said:
there is no render, I put the code in the prerender event, with the same
problem, just out of curious....this is taking me into the birth of the page
:)......
The System.Web.UI.Page class has a render method. Your page inherits
this method. An ASP.NET page has this lifecycle:
1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
G

Guest

Thanks for the detailed lifecycleof the page...Isn't everything in a web page
suppose to be inside the html and body tags...with the exception of page
directives and header...should asp.net raise an error in this case?
 
M

Martin Eyles

oz said:
Thanks for the detailed lifecycleof the page...Isn't everything in a web page
suppose to be inside the html and body tags...with the exception of page
directives and header...should asp.net raise an error in this case?

Browsers don't tend to be fussy with errors for this (although the results
of styles and scripts might be less predictable - if you are fussy about
this, you could argue that ASP doesn't produce conforming pages anyway, as
it inludes the invalid ms-positioning property in tags.).

However this is really beyond the point, so I will get back to that. You
really have a few options. If you must use respone.write mixed in with other
code, stick to using it inline, or call procedures that use it from inline

eg
webpage

<% response.write("something") %>
<% aProcedure() %>

code behind and/or server script blocks
Sub aProcedure()
response.write("something else")
End Sub aProcedure

Alternatively use only response.writes to build the whole page, including
the <html> tag etc. (This is a very bad idea).

Finally, use the new fangled way of doing things, and add bits to parts of
the page

eg.
Webpage
<div runat="server" id="theBitToAddStuffTo>
</div>

codebehind
protected withevents theBitToAddStuffTo as System.Web.UI.GenericHTML

theBitToAddStuffTo.innerHTML=theBitToAddStuffTo.innerHTML+"Some Stuff"

There are I suspect even more, possibly better ways to do it

ME
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top