[英]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. 目前,我在asp.NET中的网站项目包含5个.aspx文件。 I have defined the same functions in every .aspx file.
我在每个.aspx文件中定义了相同的函数。 What I want is a way to create my own library/file of functions, which I could include in my .aspx scripts.
我想要的是一种创建自己的函数库/文件的方法,我可以将其包含在我的.aspx脚本中。
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. 在搜索解决方案时,我只发现了动态包含ASP.NET中的文件,用于在.aspx中动态包含HTML和客户端脚本。 So it's not what I need.
所以这不是我需要的。
You can create a library very easily in ASP.NET. 您可以在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. 如果您的ASPX文件没有代码隐藏,我建议添加它,但无论是否有代码隐藏,您都需要确保在访问库类之前包含库文件的命名空间和他们的方法。
Syntax for including a namespace on a ASPX file is: 在ASPX文件中包含命名空间的语法是:
<%@ 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: 以下是我过去遇到过的一些内容:
Just create a .cs file is drop it in your App_Code directory. 只需创建一个.cs文件即可将其放入App_Code目录中。 Make your class and appropriate functions public, and you're good to go.
让你的课程和适当的功能公开,你很高兴。
follow these 5 easy-peasy steps! 按照这5个简单易行的步骤! to save tons of function re-writes between asp.net web forms!
在asp.net Web表单之间保存大量的函数重写!
1)Create a class inside your App_Data folder: (right-click app > Add > Add ASP.Net folder) (Add > New Item > Class) 1)在App_Data文件夹中创建一个类:(右键单击app>添加>添加ASP.Net文件夹)(添加>新项>类)
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: 2)在课堂内创建方法:
namespace applicationName.App_Code
{
public class ClassName
{
public string classMethod()
{
return "hello";
}
}
}
3)On the codebehind of any form, include this class header: 3)在任何表单的代码隐藏中,包括此类头:
using applicationName.App_Code; //add this
4)On the codebehind of this form, create an instance of the class: 4)在此表单的代码隐藏中,创建类的实例:
ClassName classInstanceName = new ClassName(); //add this
5)Call the method in your form codebehind 5)在表单代码隐藏中调用方法
classInstanceName.classMethod()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.