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.

1 comment:

Anonymous said...

Dear Roger,
I have a question about using .ascx controls.

Actually, I am building a dynamic CMS. I want to store the .ascx HTML & Nvelocity code into the database. After, I'll pass a ItemsCllection to inilialize the control.

My question is :
Will the Nvelocity code be processed automatically if I pick it up and place it into a PlaceHolder on the .ascx ?

Is there a way to cache the .ascx based on a parameter I can pass when creating control.