1

I have written a class library function that parses a string to a control. It takes the current page and uses page.ParseControl to parse the string as a control.

System.Web.UI.Page page = (System.Web.UI.Page) HttpContext.Current.CurrentHandler;
System.Web.UI.Control ctrl = page.ParseControl(str); 

It works fine in most scenarios except when the caller of this function is an Ajax Web Service in which case I am getting the following error: Unable to cast object of type 'HandlerWrapperWithSession' to type 'System.Web.UI.Page'. I am obviously getting this error because the CurrentHandler is not of type Page, but I need a Page object to call ParseControl.

Any ideas how I can solve this problem?

Andre
  • 1,852
  • 1
  • 16
  • 21
  • Can you give a little more detail what you're trying to do? – CodingGorilla Oct 14 '10 at 15:00
  • @Coding Gorilla I am using the parseControl method to see if the markup in the string is valid. I am also checking for certain tags that I am filtering out. – Andre Oct 14 '10 at 15:09
  • Would it suffice to simply do: `Page page = HttpContext.Current.CurrentHandler as Page`. This would result in `page == null` when it wasn't really a page. – CodingGorilla Oct 14 '10 at 15:21
  • Unfortunately not. Now I get the error: Object reference not set to an instance of an object. It really makes me wonder why parseControl is not static. – Andre Oct 14 '10 at 16:32

1 Answers1

0

You can just do:

Page p = new Page();
Control ctrl = p.ParseControl(str);
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74