Sunday, February 25, 2007

Getting Parameters to .ascx files

In .net, the preferred way of performing server side includes is to use the web control, or .ascx file. As I noted earlier .aspx files must have the html and body tags in them. but .ascx files do not. These are simply easy to use html blocks that can be plugged in to an .aspx file.

They also allow you to write code behind the html blocks, like the .aspx file does. However, passing parameters to the control is not apparent, so again hacking comes to the rescue.

Let's imagine an aspx file that selects an XmlNodeList from a document. Then each item in the node list should be passed to the control for formatting and inclusion in the webpage.

To pass that node, add the node to the Session, and then in the .ascx, always start by retrieving the value from the Session.

This creates a loose coupling between the .aspx and the .ascx which must be documented. I also like to create public string constants that are used for keys to the session context.

If there is any interest, I will post code samples.

Wednesday, February 21, 2007

Servlets in .net?

Today I am writing a website using .net 2.0, and one of the pages needs to return a .pdf rather than HTML. So the return back needs to not output the normal html headers and content. But in ASPX, every webpage must contain at least this:

But once that is emitted into the stream, I am not outputting a .pdf any more. So I needed to subvert the streaming of that content.

Here's what I did:
<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<% if (true)
{ %>
<% } %>

Once the html tags were subverted, I was able to write the Emit_Content() function in Default2.aspx.cs. Note that byte array _buff and Int32 _got are member variables.

* Write out the contents of the
* pdf to the http response socket
protected void Emit_Content()
Response.ContentType = "application/pdf";

Response.BinaryWrite(_buff, _got));
catch (Exception ex)
Response.Write("<html><body>" +
ex.Message + "</body></html>");