简体   繁体   English

如何在不同的.aspx文件中重用相同的函数或创建函数库?

[英]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: 以下是我过去遇到过的一些内容:

  1. Using a base page - you can make your aspx pages derive from a custom class that dervies from Page. 使用基页 - 您可以使您的aspx页面派生自一个来自Page的自定义类。 This way, you can call base.MethodName(). 这样,您可以调用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 ). 您的要求还将决定该类是否应该是静态/实例化的(对我来说,更像是在System.IO.FileSystem.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. 扩展方法 - 类似于#2,除了它的静态和与页面类型紧密相关。 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. 只需创建一个.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.

相关问题 如何重用相同的变量,但用于不同的类型? - How to reuse the same variable, but for different types? Usercontrol与不同的aspx,但相同的实现 - Usercontrol with different aspx, but same implementation 如何从类文件(服务器端)向.aspx文件(客户端)发出请求? - How to make request from Class files(serverside) to .aspx file(clientside)? 我如何在WPF棱镜中使用不同的ViewModel重用同一视图? - How can I reuse the same view with different ViewModel in wpf prism? 如何使用用户管理器功能制作网络核心库 - How to make a net core library with usermanager functions Aspx和aspx.cs从不同的resx文件获取字符串 - Aspx and aspx.cs getting strings from different resx files 如何为同一个按钮分配两个不同的功能? - How to allocate two different functions to the same button? 如何在C#类(.cs文件)和ASP.NET .aspx文件中使用相同的OracleMembershipProvider? - How can you use the same OracleMembershipProvider in C# classes (.cs files) AND ASP.NET .aspx files? 如何以编程方式加载aspx文件? - how to load aspx files programmatically? 如何在单击 gridview 行时调用函数。 这两个函数都放在 aspx 页面的脚本标签中 - how to call a function on click on a gridview row. both functions are placed in script tags in aspx page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM