UserControl.Page_PreRender: the Response.Write text appears before the page contents.

M

Max2006

Hi,



I have a user control that shows the contents of potentially big html files.

To save the server memory, I try to avoid reading the whole large-file's
text into a label or literal control. Therefore, I tried to use this code in
the user control's code behind:



protected void Page_Load(object sender, EventArgs e)// Also tried
Page_PreRender, same problem!

{

Response.WriteFile(HTMLFileName);

}



The code doesn't work because it dumps the file before the actual page tags.
How can I directly dump a text file at the position of the actual user
control?



Thank you,

Max
 
S

Steven Cheng[MSFT]

Hello Max,

As for dumping some text data directly in a specified postion in an ascx
usercontrol, you can consider use the inline rendering expression <%= %>
and call a helper function to output the text in it. For example:

=========ascx template=============
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TextUC.ascx.cs"
Inherits="UserControl_TextUC" %>
<table style="width:100%">
<tr>
<td >
<%= GetLargeText() %>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
==============================

======code behind==============
public partial class UserControl_TextUC : System.Web.UI.UserControl
{

protected string GetLargeText()
{
StreamReader sr = new StreamReader(@"D:\temp\my.log");

string text = Server.HtmlEncode(sr.ReadToEnd());

sr.Close();

return text;
}
}

}
============================


Because such inline render experssion <%= %> is called at the whole page's
output rendering, the data returned in the expression will be inserted at
the specfiied position in the page's output content.

As for the response.Write method, it will always write the data into the
Page's response stream directly, at generally before page's Render event,
the page's whole output content has not been flushed yet, so any data write
through "Response.Write" will occur at the top of the page output.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top