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.
Post a Comment