How can you call a static method defined in one web form code behind class from another web form (or from a non-web form class)?
The class name does not seem to be visible in other parts of the web site. Are they in a different namespace? There seems to be a namespace prefix called ASP that can reference the current class and master page classes and global.asx. But you can't reference
other web form classes.
I don't want a method in another class. I have plenty od classes in app_code that I can call from anywhere.
I want a method on the page that I can call from other classes. Specifically I want to have a method that returns the URL to redirect to that page. The method may have parameters that are translated into query string parameters on the URL. This is one example,
there are others where I would want an actual static method on the page class.
I tried creating a partial class with the same class name in a unit in app_code. That doesn't work either...
The web compilation model in vs2005 has each page getting compiled into seperate assemblies. beacause of this, you would not have a way to reference the codebehind of one page from another page or from an app_code class. if you were using a Web Application
Project (WAP), then it would use the compilation model of vs2003 and you could do what you are asking.
Thanks for that reply. I didn't know about Web Application Project. But that is not an option for me. The project is well under way in standard ASP.NET 2.0 Web Site structure.
I thought I would be able to crete a partial class in app_code and then extend it in each of my pages .cs files to add the methods I want. But even that does not work. I assume that partial classes require all partial parts to be in the same assembly. Is
there anyway to turn off the seperate assembly for each page? And compile everything into one assembly? Is the name of the assembly for a page known? Can you refer to it somehow?
Thanks for that reply. I didn't know about Web Application Project. But that is not an option for me. The project is well under way in standard ASP.NET 2.0 Web Site structure.
I thought I would be able to crete a partial class in app_code and then extend it in each of my pages .cs files to add the methods I want. But even that does not work. I assume that partial classes require all partial parts to be in the same assembly.
a partial class is simply a way to create a single class from multiple source files. it has no impact of references or how the class is ultimately compiled.
skysailor
Is there anyway to turn off the seperate assembly for each page? And compile everything into one assembly?
I found that if I put a <%@ Reference Page="rrrr.aspx" %> driective in one page then I can refer to the other page class file directly from the first page using the ASP namespace. That is exactly what I wanted. However it can't be used from app_code classes.
And the killer - it can't be used if you Publish your web site!! So close. I have to publish/compile my web site.
skysailor
Member
1 Points
13 Posts
Calling Static methods on Code Behind class of WebForm
Apr 19, 2007 02:06 AM|LINK
How can you call a static method defined in one web form code behind class from another web form (or from a non-web form class)?
The class name does not seem to be visible in other parts of the web site. Are they in a different namespace? There seems to be a namespace prefix called ASP that can reference the current class and master page classes and global.asx. But you can't reference other web form classes.
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Calling Static methods on Code Behind class of WebForm
Apr 19, 2007 02:27 AM|LINK
create a class/classes in your app_code folder for your site wide common methods.
move your static method to the new class.
classes in app_code are available to the entire app.
skysailor
Member
1 Points
13 Posts
Re: Calling Static methods on Code Behind class of WebForm
Apr 19, 2007 04:45 AM|LINK
I don't want a method in another class. I have plenty od classes in app_code that I can call from anywhere.
I want a method on the page that I can call from other classes. Specifically I want to have a method that returns the URL to redirect to that page. The method may have parameters that are translated into query string parameters on the URL. This is one example, there are others where I would want an actual static method on the page class.
I tried creating a partial class with the same class name in a unit in app_code. That doesn't work either...
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Calling Static methods on Code Behind class of WebForm
Apr 19, 2007 11:24 AM|LINK
The web compilation model in vs2005 has each page getting compiled into seperate assemblies. beacause of this, you would not have a way to reference the codebehind of one page from another page or from an app_code class. if you were using a Web Application Project (WAP), then it would use the compilation model of vs2003 and you could do what you are asking.
skysailor
Member
1 Points
13 Posts
Re: Calling Static methods on Code Behind class of WebForm
Apr 20, 2007 12:11 AM|LINK
Thanks for that reply. I didn't know about Web Application Project. But that is not an option for me. The project is well under way in standard ASP.NET 2.0 Web Site structure.
I thought I would be able to crete a partial class in app_code and then extend it in each of my pages .cs files to add the methods I want. But even that does not work. I assume that partial classes require all partial parts to be in the same assembly. Is there anyway to turn off the seperate assembly for each page? And compile everything into one assembly? Is the name of the assembly for a page known? Can you refer to it somehow?
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Calling Static methods on Code Behind class of WebForm
Apr 20, 2007 12:36 AM|LINK
a conversion capability exists: http://west-wind.com/WebLog/posts/13132.aspx
bu sure to read the link to Scott Guthries walk through.
a partial class is simply a way to create a single class from multiple source files. it has no impact of references or how the class is ultimately compiled.
That is what a Web Application Project does.
skysailor
Member
1 Points
13 Posts
Re: Calling Static methods on Code Behind class of WebForm
Apr 20, 2007 12:50 AM|LINK