This is not a good practice. If both pages want to share this method, you can put them in another library of classes / classes and create an instance and call it from both pages.
The approach you will use is determined by the nature of the method; consider the context. If we are in the context of Page
, then itβs best to think that the functionality here should be related to the actions that should be performed on the page, that is, the view (GUI rendering).
If functionality is associated with a view, consider using a common class or a common base class .
In this question, the sharing method is Connect
β if it connects to a service or database for data, then consider encapsulating this code as an additional library; this library can be reused in several projects (regardless of the display style), and the logic will be independent of the display; eg:
public partial class MyCodeBehindCS : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyNamespace.MyCustomClass myClass = new MyNamespace.MyCustomClass(); myClass.Connect(); var myResult = myClass.DoSomething(); } }
Then you can do whatever you want with myResult.
source share