2007.10.18

C# - Embedding dynamic ASPX in a .NET WebControl

Kategori: Programmering — Michael Schøler, Kl. 18:56:22

Oh yeah!I have been looking for a way to embed non-static contents into a custom WebControl for a while now without any luck. In fact, I have seen quite a lot of “this cant be done” forum postings, even from MS MVP’s and official staff members from Microsoft Online Support. What a bummer :-/ …… hint: I’ve found a solution, read on.

It’s very easy to embed static contents such as HTML, CSS, images and JavaScript (even Ajax capable) - but directly embedded APSX files for dynamic generation of HTML contents eg. for your WebControl’s Ajax capabilities, dynamically created images, and so forth turns up nothing from extensive “googling”.

I rejected to accept that it is not possible to have dynamic contents in a WebControl component. I mean, it is a common thing to use dynamic and interacting ressources these days, especially since Ajax hit the developer windshield at 220 mph and squashed out all over it, all sticky n’ gooey. So, … I kept on trying to accomplish the task at hand; how to embed ASPX into a C# custom WebControl compiled as a standalone DLL (managed code).

And tada - I’ve now found a 100% working solution! Take that!

You can take advantage off the fact that .aspx and aspx.cs files can be compiled by the server side JIT compiler (Just In Time compilation). Realizing that, it’s a fairly simple step to envision your WebControl injecting .aspx and .aspx.cs files directly to the server directory.

You can quite simply place any needed .aspx and code behind files as hidden ressources in your WebControl and use eg. a StringWriter in the WebControls Render or Page_Load method to dump the file to a server directory residing in the web application. A word of advice is to use very specific and carefully thought through filenames for these injected files, as it would be unwise to overwrite existing files in the web application.

To avoid forcing on a JIT compilation for each page hit on the web application using your WebControl, you should seriously consider putting in some simple logic capable of detecting if the files have allready been injected. Should the files become overwritten, they will be JIT compiled when accessed.

After you have put the above code in placein your WebControl, the dynamically generated aspx file can be invoked directly by your WebControl as you would invoke any of the aspx files already available in the web application.

I’ll post a generalized code example here soon. My current working example is not suitable as a proof-of-concept example as it is a part of a larger system design.

Best regards,
Michael Schøler

Ingen kommentarer »

Endnu ingen kommentarer.

RSS feed for kommentarer til dette indlæg. TrackBack URI

Send kommentar

*
For at bevise at du er en person, og ikke en spam-robot, skal du indtaste dette kodeord. Bemærk at kodeordet altid starter eller slutter med et tal. Klik på billedet for at få kodeordet læst op.
Click to hear an audio file of the anti-spam word