简体   繁体   English

我可以让两个aspx页面共享相同的代码隐藏文件吗?

[英]Can I have two aspx pages share the same code-behind file?

For split testing, I have three pages which do exactly the same thing but look visually very different (significant changes to both HTML and CSS). 对于拆分测试,我有三个页面,它们的功能完全相同,但外观却截然不同(对HTML和CSS都进行了重大更改)。

Is there a way I can create two or more ASPX pages which use exactly the same code behind? 有没有一种方法可以创建两个或多个在后面使用完全相同的代码的ASPX页面? Whatever I try doesn't seem to work due to how aspx pages are compiled. 由于aspx页的编译方式,我尝试执行的任何操作似乎均不起作用。 Ideally I'd live to have page1.aspx, page2.aspx both have Inherits=" CommonClassName " but then I just can't get it to compile. 理想情况下,我将拥有page1.aspx,page2.aspx都具有Inherits =“ CommonClassName ”,但后来我无法将其编译。

you can create a new class which inherit from Page 您可以创建一个继承自Page的新类

public class ParentPage : System.Web.UI.Page

in there put whatever you want to share. 在其中放任何您想分享的内容。 then have your pages inherit from there: 然后让您的页面从那里继承:

public partial class PageFirst : ParentPage 
public partial class PageSecond : ParentPage 

Put your common logic into utility classes that all three visually different pages can call to exercise that functionality. 将您的通用逻辑放入实用工具类中,这三个在视觉上不同的页面都可以调用以行使该功能。

For example, 例如,

public static bool SaveValueToDatabase(string theValue)
{
    // Call database from here
}

Now in your three pages, you can invoke this shared logic, like this: 现在在您的三个页面中,您可以调用此共享逻辑,如下所示:

var success = SaveUserToDatabase(TextBox1.Text);

if(!success)
{
    // Alert user of failure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM