Putting aside any debate about whether you should even use Response.Write in the HTML portion of an .aspx, in a project I am working on variables from the code behind are being displayed on the front-end using Response.Write. It used to work fine but something in the project changed recently (another development team was working on it, so I don't know exactly what happened) but now all the Response.Write code blocks are displaying at the top of the page. The only clue I have to what might have changed is that some recent AJAX functionality was added to the project.
-
2try using ASP.Net server side tag `<%= variableValue %>` instead of `Response.Write(variableValue)` – Naveed Butt Aug 03 '11 at 18:40
1 Answers
Response.Write
writes to the response stream. The reason that your data is added on the beginning of the response stream can only be that the event where your statements are triggered is before any of the Response.Write
of ASP.NET start.
You should typically override Render
, RenderChildren
or RenderControl
to put your Response.Writes in, and make sure to call the proper parent methods, otherwise only your statements are visible. Alternatively, put your Response.Write inside server tags <% %>
or use the suggestion of Naveed (which btw translates internally into a Response.Write
anyway, but is much clearer and easier to write).
EDIT summary of the extended chat below: the cause was found in the Telerik RadAjax.Net2 control. On removing that, the issue went away. Solved by Andreas himself.

- 56,041
- 24
- 146
- 247
-
Hey. Thanks a lot for your help. Unfortunately the Repsonse.Write are within server tags already and it does not work using the <%= => construct either. What is strange is that it worked at one point and something changed to break it. I've narrowed it down a but further to find that the Response.Write is being rendered outside of a specific control in the page where the HTMLTextWriter is being altered like this: – Andreas Aug 03 '11 at 20:04
-
@User: the part after "like this" is missing from your comment. But you mention "specific control". Is it possible that your code is not inside a page, but inside a control (user control, server control, masterpage)? A simple way to find out what's happening is creating a breakpoint on the `Response.Write` (click F9). – Abel Aug 03 '11 at 21:09
-
Sorry. It wouldn't let me submit the rest of text and I got side tracked. The answer to your question is yes, the Response.Write is being included within a User Control that is being dynamically included within a master page. – Andreas Aug 04 '11 at 17:01
-
The answer to your question is yes, the Response.Write is being included within a User Control that is being dynamically included within a master page. The control that wraps most of the content in the masterpage is called ActionlessForm and it overrides the HTMLForm.RenderAttributes method with some code like: writer.WriteAttribute("onsubmit", "javascript:if (typeof(WebForm_OnSubmit) == 'function') { return WebForm_OnSubmit(); }"); base.Attributes.Remove("onsubmit"); The Response.Write text is appearing outside of this control when the page is rendered – Andreas Aug 04 '11 at 17:49
-
A MasterPage is itself a control that gets special treatment and is loaded and rendered before other controls on the page. It looks like RenderXXX is called too early by user code (either yours or from the new AJAX functionality), or your usercontrol isn't properly designed, resulting in strange behavior in particular circumstances. To find out, create a minimalistic control from scratch, see if it has the same behavior and go from there, meanwhile setting your breakpoints properly. Without seeing your whole project I'm afraid I can't give you much more than the above regular advice. – Abel Aug 04 '11 at 20:03
-
@user As a suggestion, try posting a minimal example of your problem at http://codereview.stackexchange.com/ – Abel Aug 04 '11 at 20:09
-
1We have discovered that the problem occurs because the project is using the Telerik RadAjax.Net2 control and this is somehow effecting how the Response handler is performing. If we disable the RadAjax controller the Response.Write works properly. Thank you Abel for all your help. – Andreas Aug 05 '11 at 00:52
-
@Andreas: Still strange. We use Telerik controls daily and never came across this oddity. But we never use Response.Write (but still, we have code with `<#=`, which is essentially the same). But glad you found the culprit! – Abel Aug 05 '11 at 15:29