locked
Calling Static methods on Code Behind class of WebForm RRS feed

  • Question

  • User-805177867 posted

    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.

     

    Wednesday, April 18, 2007 10:06 PM

Answers

  • User-158764254 posted

    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.

    a conversion capability exists: http://west-wind.com/WebLog/posts/13132.aspx
    bu sure to read the link to Scott Guthries walk through.

    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.

    Is there anyway to turn off the seperate assembly for each page? And compile everything into one assembly?

    That is what a Web Application Project does.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 19, 2007 8:36 PM

All replies

  • User-158764254 posted

    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.

     

    Wednesday, April 18, 2007 10:27 PM
  • User-805177867 posted

    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...

    Thursday, April 19, 2007 12:45 AM
  • User-158764254 posted

    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.

     

    Thursday, April 19, 2007 7:24 AM
  • User-805177867 posted

    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?

    Thursday, April 19, 2007 8:11 PM
  • User-158764254 posted

    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.

    a conversion capability exists: http://west-wind.com/WebLog/posts/13132.aspx
    bu sure to read the link to Scott Guthries walk through.

    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.

    Is there anyway to turn off the seperate assembly for each page? And compile everything into one assembly?

    That is what a Web Application Project does.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 19, 2007 8:36 PM
  • User-805177867 posted
    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.
    Thursday, April 19, 2007 8:50 PM