RadControls version
|
Q1 - 2012+
|
.NET version |
All
|
Visual Studio version |
All
|
programming language |
C#
|
browser support |
all browsers supported by RadControls
|
PROJECT DESCRIPTION
Due to security limitations with browsers and JavaScript, the RadEditor control doesn't natively support adding stylesheets from different domains to the editor control. Getting around this is actually very easy though and should work in any version of .Net, although my development was with version 4 and VS11. The only requirement for this solution is knowing the name and location of the stylesheet you need and a basic understanding of HTTP Handlers.
To start, you will need to create a generic HTTP Handler with any name you wish - I chose "getstyles.ashx". Next, you'll need to add automatic caching/expiration to the handler so you always get the latest copy of the stylesheet. This becomes essential if you pass query string values to this URL (detailed at the end of this tutorial) because the browser will cache the file you return and subsequent calls with different query string parameters will always load the previously cached version. To do this, add this inside the ProcessRequest(HttpContext) method:
After those lines, you build the real meat of the handler. Assuming you know the name/location of the stylesheet you wish to link to, you just write a simple HttpWebRequest to connect to that location, download the CSS file and then return it's contents back to the editor. For the scenario I implemented this with, I used query string values to further customize which CSS file I needed to pull down (based on individual sites inside a common backend implementation). The full code for making the request is below (including the caching):
If you passed query string values to the handler, you can access them inside the HttpContext value passed into ProcessRequest like so:
Once you've implemented the handler, the rest is very simple. Just link to the handler in your RadEditor code as a standard CSS file like so:
C# Version
C# Version with query string values
ASPX/Markup Version (doesn't support dynamic query string values):
As with any solution that involves dynamically pulling external resources, make sure you have permission from the site owner to retrieve the CSS files you're utilizing in your editor.
Hopefully this helps someone out!