Calling Static methods on Code Behind class of WebForm

Last post 04-19-2007 8:50 PM by skysailor. 6 replies.

Sort Posts:

  • Calling Static methods on Code Behind class of WebForm

    04-18-2007, 10:06 PM
    • Member
      1 point Member
    • skysailor
    • Member since 04-18-2007, 10:01 PM
    • Posts 13

    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.

     

  • Re: Calling Static methods on Code Behind class of WebForm

    04-18-2007, 10:27 PM
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    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.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Calling Static methods on Code Behind class of WebForm

    04-19-2007, 12:45 AM
    • Member
      1 point Member
    • skysailor
    • Member since 04-18-2007, 10:01 PM
    • Posts 13

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

  • Re: Calling Static methods on Code Behind class of WebForm

    04-19-2007, 7:24 AM
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    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.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Calling Static methods on Code Behind class of WebForm

    04-19-2007, 8:11 PM
    • Member
      1 point Member
    • skysailor
    • Member since 04-18-2007, 10:01 PM
    • Posts 13

    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?

  • Re: Calling Static methods on Code Behind class of WebForm

    04-19-2007, 8:36 PM
    Answer
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs
    skysailor:

    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.

    skysailor:

    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?

    That is what a Web Application Project does.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Calling Static methods on Code Behind class of WebForm

    04-19-2007, 8:50 PM
    • Member
      1 point Member
    • skysailor
    • Member since 04-18-2007, 10:01 PM
    • Posts 13
    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.
Page 1 of 1 (7 items)