By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Aug 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Aug 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.