Rendering Controls to a String

posted on 07/13/07 at 12:35:17 am by Joel Ross

This is probably my favorite little trick. It allows you to take a control and render it as a string.

Why is this useful? I can think of several spots you could use this. First, now that AJAX is much more prevalent, you could use it in an AJAX call - you'd have a custom method that you call to update a grid (rather than using an UpdatePanel). Your web service could use this trick to give the client the updated HTML needed to write back to the web page. Or you could just use this with a web service to allow another site to render data from your site - without forcing the other site to deal with how to render it.

But my personal favorite is what I did with Tourney Logic and Tourneytopia's standings updates. Whenever a round is complete, we send out emails to everyone with updates of the current standings. We already have a user control to do this (with a databound grid on it), so why duplicate effort? We just databind the grid and render the control to a string, and use that as part of the body for the email. So now our ASP.NET server controls are multi-purpose.

Technorati Tags:

Categories: ASP.NET