views:

129

answers:

3

I have an asp.net page with a code-behind class definition as follows:

public partial class examplepage : System.Web.UI.Page

I'd like to set a public property within the page that I can reference from other classes. My understanding is that if I cast to examplepage then I should be able to get at the public property that is specific to example page, as in:

string test=((examplepage)HttpContext.Current.Handler).propertyX;

However, when I try casting as above the compiler does not recognise examplepage. Can anyone tell me how I can cast? I have no specific namespaces defined.

Thanks

A: 

If your class is in a different namespace, you have to specify the namespace for your web pages:

string test = ((MyWebApp.examplepage)HttpContext.Current.Handler).propertyX;
Guffa
I don't have any namespace definitions though. All the relevant code has no namespace declarations at all. I tried adding one but it didn't seem to help so i removed it.
DEH
@DEH: There may be an implicit namespace. Check the properties for the web project to see if there is a defualt namespace. You can also check if there is an ASP namespace that contains the web pages.
Guffa
@Guffa: Thanks - please see my answer re partial classes. Any idea how I can remove the partial keyword?
DEH
A: 

I've just read that ASP.NET 2.0 does not support casting on partial classes because at compile time the class "does not exist".

Assuming this to be true, I'd like to remove the "partial" keyword from the class definition. The compiler won't let me do this though, stating that another partial definition exists elsewhere. Is this the designer? Anyone know how I can avoid partial classes so I can cast as I want to?

DEH
But partial classes DO exist at compile time... I don't think trying to remove "partial" is going to help you solving your problem.
rmacfie
ASP.NET uses partial classes to put the designer definitions in a separate file. The statement that you can't cast partial classes is incorrect, I just tried it to verify, and it works just fine. The error that you get is because the class that you are trying to cast to is unknown, it's not because the cast is not allowed.
Guffa
A: 

In case anyone's interested the following is what I switched to:

public partial class examplepage : System.Web.UI.Page, ISomeStuff

and

string test = ((ISomeStuff)HttpContext.Current.Handler).propertyX; 

Thanks for the advice folks

DEH