Shared Content Across Sites with ASP.NET User Controls and JavaScript
March 15th, 2008Rendering a User Control in ASP.NET using a web service or a generic handler is easy. Taking the string that is generated with a User Control and then serializing it to be embedded in JavaScript is also easy. Both combine to what you will need to create custom ads that are exactly like how Google Ads are served. This technique can also be used to share a common piece of markup across many web sites. As the central web site is updated so are all sites that reference it.
The first method below will render a User Control to a string. The second method will serialize that string for use with JavaScript as a JSON value. The value can then be included in a dynamically generated script for use from remote sites. I had some early troubles with the DataContractJsonSerializer which replaces the deprecated JavaScriptSerializer but eventually I got the following code to work reliably. Previously I created something similar but without User Controls a while back for the local .NET User Group to show banners for upcoming meetings.
