Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何代码重用apsx?_C#_Asp.net_.net_Design Patterns - Fatal编程技术网

C# 如何代码重用apsx?

C# 如何代码重用apsx?,c#,asp.net,.net,design-patterns,C#,Asp.net,.net,Design Patterns,我知道在C#中我可以创建工厂,但我不知道如何在aspx中重用代码。我的代码最初只用于ARList,但现在有了IcnList。我想做一个switch语句,但是没有更好的代码吗 功能 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ARExtractionController arControl

我知道在C#中我可以创建工厂,但我不知道如何在aspx中重用代码。我的代码最初只用于ARList,但现在有了IcnList。我想做一个switch语句,但是没有更好的代码吗

功能

 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;
            }
        }
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
{
ARExtractionController arController=新的ARExtractionController();
Dictionary ARDictionary=arController.GetTickets();
List sortedARList=新列表();
foreach(ARDictionary中的KeyValuePair kv)
{
分类列表。添加(千伏键);
}
sortedARList.Sort();
sortedARList.Reverse();
ARList.DataSource=sortedARList;
ARList.DataBind();
ARList.Items.插入(0,“”);
ARList.AutoPostBack=true;
}
}

如果您只是希望能够重用代码,请在项目中为实用工具方法添加一个类,并将其转储到其中供两个页面使用

如果您试图重用代码和关联的UI,请查看允许您这样做的用户控件(ascx文件)


从这个问题上看,不清楚哪一个适合您。

您应该能够从单个页面继承代码以重用代码

aspx文件的顶部:

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

在TicketExtractionWeb中,创建一个BasePage类

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;
    }
}
还有一个页面(我们称之为MyPage):

因此,当您创建另一个aspx页面时,它将默认为:

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

只需将
:System.Web.UI.Page
更改为
:BasePage

如果要重用html,请使用一个用户控制文件(.ascx),该文件可以多次重用


你所说的在技术上是正确的,但我认为它不可能真正做到OP真正需要的。@tomfanning在技术上是正确的。最好的一种是正确的!哦,等等,我明白你的意思了。当然,除非你可以有多个继承属性!(我98%肯定你不能)@tomfanning九小时前你在哪里。我们也经历了同样的事情,试图重用aspx文件中的代码。在放弃并尝试其他操作之前,最终尝试在dll中嵌入ascx文件。这里的答案肯定是“在项目中创建一个类”或“创建一个包含一些类的程序集并添加对它的引用”。ASP.NET Web应用程序项目没有什么特别之处…可能是
public class MyNewPage : System.Web.UI.Page
{
    // ...
}