public partial class DemoPage : System.Web.UI.PageBut what if you're abstracting things so that a library object has to be able to render itself as HTML?
{
protected void Page_Load(object sender, EventArgs e)
{
LabelControl.Text =
Server.HtmlEncode( LibraryClassInstance.Content );
}
// rest of the implementation goes here
}
The problem is all about getting at the HtmlEncode method if you've not got a Server object handy. The Server object is actually a singleton instance of the HttpServerUtility class exposed by the ASP.Net environment from the HttpContext in which the page is running.
HOWEVER, the HttpContext class exposes a static method to return the current context instance, including the associated HttpServerUtility instance, so all you have to do in the library is the following
namespace demoVoila - HTML (and URL) encoding and de-coding at your fingertips.
{
class LibraryClass
{
public string GetHtmlSafeContent()
{
return HttpContext.Current.Server.HtmlEncode(this.Content);
}
// remainder of the implementation goes here
}
}
No comments:
Post a Comment