开发者

How to reuse same functions in different .aspx files or make function library?

At the moment my website project in asp.NET consists of 5 .aspx files. I have defined the same functions in every .aspx file. What I want is a way to create my own library/fi开发者_JS百科le of functions, which I could include in my .aspx scripts.

While searching for solution I've only found this Dynamically Including Files in ASP.NET Which is used to dynamically include HTML and client-side scripts in .aspx. So it's not what I need.


You can create a library very easily in ASP.NET.

Decide on a namespace and create new classes under that namespace (generally each class in a separate file but it is not enforced)

If your ASPX files don't have code-behind I would recommend to add it, however regardless of whether there is a code-behind or not you would need to make sure you include the library files' namespace before you can access the library classes and their methods.

Syntax for including a namespace on a ASPX file is:

<%@ Import Namespace="mylibrarynamespace" %>

In the code-behind is:

using mylibrarynamespace;

Hope it helps.


How about making a base class from which all of your page classes derive? That seems like the simplest way to go.


There are a lots of approaches you can take to sharing code between pages. It really depends on what type of functionality you are trying to achieve.

Here are some that I've encountered in the past:

  1. Using a base page - you can make your aspx pages derive from a custom class that dervies from Page. This way, you can call base.MethodName(). This is especially useful if you would need access to private/protected members/events.
  2. Put methods in a class - you can create a custom class that houses your methods. I suggest doing this if all the methods are related and it makes sense to bundle them in one class. Your requirements will also dictate whether the class should be static/instantiated (to me, its more like choosing between System.IO.File vs System.IO.FileInfo). Downside to this, is you cant access any private members of the page or fire any of its events.
  3. Extension methods - its kind of like #2, except its static and tied closely to the Page type. The syntax makes it more readable (in my opinion). However, there are certain design guidelines that you should consider.


Just create a .cs file is drop it in your App_Code directory. Make your class and appropriate functions public, and you're good to go.


follow these 5 easy-peasy steps! to save tons of function re-writes between asp.net web forms!

1)Create a class inside your App_Data folder: (right-click app > Add > Add ASP.Net folder) (Add > New Item > Class)

important! when you add these classes, you have to switch the build action to "compile" instead of "content" in the properties of the file, otherwise your codebehind won't recognize the imported namespaces.

2)Inside the class, create methods:

namespace applicationName.App_Code
   {
       public class ClassName
           {
               public string classMethod()
                  {
                    return "hello";
                  }
           }
   }

3)On the codebehind of any form, include this class header:

using applicationName.App_Code; //add this

4)On the codebehind of this form, create an instance of the class:

ClassName classInstanceName = new ClassName(); //add this

5)Call the method in your form codebehind

classInstanceName.classMethod()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜