Wednesday, September 18, 2024

Write Info Directly to the HTTP Output Stream

This article is an excerpt from the book: Murach’s ASP.NET 2.0 Web Programming with C# 2005.

Another way to display information as a program executes is to write it directly to the HTTP output stream. To do that, you use the Write method of the HTTP Response object as shown in figure 4-17. When you use this technique, you’ll want to be sure to remove any statements you’ve added when you finish testing your application.

At the top of this figure, you can see a Cart page that includes output that indicates the number of items that are currently in the shopping cart. To generate this output, I added a Response.Write method to the Page_Load event handler of the page. As you can see, this event handler uses the Count property of the Cart object to determine the number of items that are in the cart.

Notice that the text you include on the Write method can include HTML tags. For example, the Write method shown here includes a
tag so that the item count is followed by a blank line. Also note that the output you write to the output stream is always added to the beginning of the stream. Because of that, it always appears at the top of the browser window.

The Cart page with output generated by Response.Write

A Page_Load event handler that writes to the HTTP output stream

protected void Page_Load(object sender, EventArgs e)
{
this.GetCart();
if (!IsPostBack)
{
this.DisplayCart();
Response.Write("Items in cart = " + cart.Count + "<br />");
}
}

Code that writes HTML output from another class

HttpContext.Current.Response.Write("Now updating file.<br />");

Description

    The Write method of the HttpResponse object provides a convenient way to write data directly to the HTTP output stream. The output can include any valid HTML.

    To access the HttpResponse object from the code-behind file for a web page, you use the Response property of the page. To access this object from a class that doesn’t inherit the Page class, you use the Response property of the HttpContext object for the current request. To access this object, you use the Current property of the HttpContext class.

    The HTML output you add to the HTTP output stream using the Write method of the HttpResponse object appears at the beginning of the output stream. As a result, the output from the Write method always appears at the top of the page in the browser window.

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark Murdok:

Joel Murach has been writing and editing for more than 10 years. During that time, he sharpened his programming skills as a contract programmer in San Francisco and his instructional skills as a trainer for HarperCollins Publishing. He always brings a vision to his projects that leads to improved effectiveness for his readers.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

There are currently no active listings that match your search criteria.