Yamcha
Yamcha

Reputation: 1334

Declare functions in ASP.NET WebPages

Is it possible to declare functions (as you would do in C# not javascript) or create classes in ASP.NET Web Pages?

How about in MVC, in the View files?

If not, is there anyway to do event handling in Web Pages? (Again, NOT javascript eventhandling)

Upvotes: 0

Views: 2049

Answers (1)

Mike Brind
Mike Brind

Reputation: 30035

You can create your own functions within .cshtml files. This article explains the functions keyword and how to use it: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix. If you include it in the .cshtml file that also makes use of the function, you cannot call it from other pages. If you wanted to do that, you can add a function to a .cshtml file and then put that in App_Code.

There are no events as such in ASP.NET Web Pages. You can check to see if a page has been posted back using the IsPost property.

If you want to make use of reusable utility methods in MVC, you are better off taking the more traditional approach of creating static classes and adding your methods there. App_Code is really for Web Site projects (ASP.NET Web Pages) as opposed to Web Application projects (whcih is what MVC apps are).

Upvotes: 2

Related Questions