1

My application use a masterpage which the ContentPlaceHolder align is center. But whenever, I use Response.Write() function to write something on screen, the whole page just changed to align left. I'm thinking, jquery ajax function could let me write html into some part of the page without destroy align. But I'd like to know if there is better solution. Any idea? Here's the code in code page:

 DirectoryInfo di = new DirectoryInfo("e:/asdf");
 FileInfo[] rgFiles = di.GetFiles("*.*");
 if (rgFiles != null)
 {
     sb.Append("<span class='SubTitle'>Your attachments list:</span>");
     foreach (FileInfo fi in rgFiles)
     {
         sb.AppendFormat("<br><a href='e:\\asdf\\" + fi.Name + "'>" 
                         + fi.Name + "</a>");
     }
 }
 Response.Write("<span style='position:absolute;top:200px;left:200px'>" 
                 + sb + "</span>");
Homam
  • 23,263
  • 32
  • 111
  • 187
Steven Zack
  • 4,984
  • 19
  • 57
  • 88
  • Could you post your code. There is a multitude of reasons for this. My guess would be your response.write is causing a opening/closing HTML tag to be missed. – Rory McCrossan Apr 27 '11 at 20:41
  • @ Rory: I think no need, just create a new web application with the default template of VS and write anything in `Response.Write` you'll have the problem. – Homam Apr 27 '11 at 20:46
  • @Homam:That's right! Just avoid to use this function. – Steven Zack Apr 27 '11 at 20:54

4 Answers4

3

Response.Write() writes data directly to the output stream, so depending om when you call it in the page life cycle, the data can end up enywhere on the page any destroy the markup.

Magnus
  • 45,362
  • 8
  • 80
  • 118
  • As I tried, wherever you add something in `Response.Write()` the align will be changed to left. – Homam Apr 27 '11 at 20:45
2

Avoid using Response.Write() to add content to the page, rather, you should add content to a server control.

In the web form:

<div runat="server" id="test" class="someclass"></div>

In the code behind:

test.innerHTML += somecontent;

This will keep your content within the CSS definitions you set for that server control.

brendan
  • 29,308
  • 20
  • 68
  • 109
Lactose
  • 695
  • 1
  • 7
  • 15
1

An easy way to do this is to place a Panel on your page and add a literal control to your panel like this:

myPanel.Controls.Add(new LiteralControl("Some text to write out"));

This way the text will appear exactly where the Panel is placed on the page.

brendan
  • 29,308
  • 20
  • 68
  • 109
0

I usually do this by adding a LiteralControl to a placeholder or panel asp control.

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolder" Runat="Server">

for the aspx page (assuming you're using a master page here). and then:

LiteralControl lc = new LiteralControl("<h1>Some HTML</h1>");
this.Content1.Controls.Add(lc);

In your code-behind.

Or, if you just want to add it to a panel:

<asp:Panel ID="Panel1" runat="server"></asp:Panel>