How to encode apsx reuse?

I know that in C # I can do a factory, but I don't know how to reuse code in aspx. My code was originally intended only for ARList, but now has an IcnList. I was thinking of creating a switch statement, but is there anything better for the code?

Function

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ARExtractionController arController = new ARExtractionController(); Dictionary<int, string> ARDictionary = arController.GetTickets(); List<int> sortedARList = new List<int>(); foreach (KeyValuePair<int, string> kv in ARDictionary) { sortedARList.Add(kv.Key); } sortedARList.Sort(); sortedARList.Reverse(); ARList.DataSource = sortedARList; ARList.DataBind(); ARList.Items.Insert(0, " "); ARList.AutoPostBack = true; } } 

enter image description here

+4
source share
3 answers

In TicketExtractionWeb, create the BasePage class.

BasePage.cs:

 public class BasePage : System.Web.UI.Page { // Add methods that are used on all your pages in here. protected DateTime GetCurrentDate() { return DateTime.Now; } } 

And the page (we will call it MyPage):

 public class MyPage : BasePage { protected Page_Load(object sender, EventArgs e) { var currentDate = this.GetCurrentDate(); } } 

Therefore, when you create another aspx page, it will be by default:

 public class MyNewPage : System.Web.UI.Page { // ... } 

Just change : System.Web.UI.Page to : BasePage

+2
source

If you just want to be able to reuse the code, add the class to your project for utility methods and upload it there for both pages.

If you are trying to reuse the code and its associated user interface, look in User Controls (ascx files) that let you do just that.

It is not clear what suits you from the question.

+2
source

You must inherit from one page to reuse the code.

Top of the aspx file:

 <%@ Page Language="C#" AutoEventWireup="true" Inherits="MyNamespace.CommonCode" %> 
+1
source

Source: https://habr.com/ru/post/1436696/


All Articles