两个 .aspx 页面使用相同的 .cs 文件,好主意吗?

发布于 2024-11-19 19:11:53 字数 796 浏览 0 评论 0 原文

这里有类似的帖子。我只是想澄清一些事情。我在一个测试项目中实现了这个。我创建了两个页面 page1.aspx 和 page.aspx 除了名称之外都是彼此的副本。我对它们进行了配置,以便它们使用相同的后端编码文件。

page1.axpx->页面.aspx.cs page2.aspx-> (上面的代码)

我的问题是,

  1. 避免额外代码并增强维护是一个好主意吗?

  2. 虽然在上面的测试项目中一切正常。在我的实际项目中,我遇到了这个非常常见的错误

当前上下文 asp 中不存在名称 'xxxx'

由于上述错误,代码将无法编译。如果我从浏览器运行该应用程序,它确实可以工作(或者看起来可以)。我的问题是,为什么我在一个应用程序中没有收到此错误,而在另一个应用程序中却没有收到此错误。此问题现已修复。但想知道为什么我首先会遇到这个错误,然后到处玩它,错误就消失了。

我相信这确实是 Microsoft 的一个错误。如何?我在这里解释一下。

Page1.aspx.cs实际上是使用Page.aspx页面而不是page1.aspx控件。智能感知仅显示Page1.aspx控件(如果将控件添加到page2,则智能感知中不会显示该控件,如果将其添加到Page1,则会显示)。由于两个页面上的控件完全相同(包括名称和 ID),因此它们能够奇迹般地工作。有时编译器不喜欢它并且会给你错误(没有明显的原因)。微软应该更优雅地解决这些问题,而不是把它扔给我们。

所以它已经为我解决了,但任何人都可以解释这种神秘的行为。

There are similar posts here. I just want to clarify something. I implemented this in a test project. I created two pages page1.aspx and page.aspx are copies of each other except names. I configured them so that they use the same backend coding file.

page1.axpx -> page.aspx.cs
page2.aspx -> (above code)

My question is,

  1. is it it a good idea to avoid extra code and enhance maintenance?

  2. While everyting works in test project above. In my actual project, I get this very common error

The name 'xxxx' does not exist in the current context asp

The code will not compile because of the above error. If I run the application from the browser, it does work (or seems to be). My question is, why I am not getting this error in one application and not the other. This issue has been fixed now. But want to why I get this error in the first place and then playing with it here and there, the error disappears.

I believe this really is a Microsoft bug. How? I explain it here.

Page1.aspx.cs is really using Page.aspx page and not page1.aspx controls. The intellisence show only Page1.aspx controls (if u add control to page2, it wont be shown in intellesense, if you add it to Page1, it will be shown). Since the controls on both the pages are exactly the same (including names and IDs), they miraculously work. Sometimes the compiler don't like it and will give you error (for no apparent reasons). Microsoft should address these issues more elegantly and not throw it on us.

So it is solved for me but can anyone explain this mysterious behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦归所梦 2024-11-26 19:11:54

最好将这些内容放在一个用户控件中并使用该用户控件在两页中。这样做维护起来会容易很多。

Its better to put the stuff in one user control and use that user control in both pages. Maintenance will be much easy by doing this.

送君千里 2024-11-26 19:11:54

我认为您应该让页面在文件后面有自己的代码,并将大部分重复的代码放在一个单独的类中,您可以从两个页面使用该类。

其背后的原因是,对于那些没有深入参与该项目的人(比如您,几个月后......)来说,很容易编辑后面的代码,而没有意识到它会影响多个页面。


当您手动更改页面或代码隐藏文件中的类名称时,包含部分类的隐藏设计文件可能不会更新。您可以通过在项目中搜索类名或单击指向该文件的错误消息来访问隐藏文件。

I think that you should let the pages have their own code behind files, and put most of the duplicated code in a separate class that you can use from both pages.

The reasoning behind this, is that it's easy for someone who is not deeply involved in the project (like you, a few months from now...) to edit the code behind without realising that it affects more than one page.


When you manually change the class names in pages or code behind files, the hidden design file that contains a partial class might not be updated. You can get to the hidden file by searching for the class name in the project, or by clicking an error message that leads to the file.

别理我 2024-11-26 19:11:54

没有得到微软的真正支持,

我与微软讨论了这个问题,以下是我从他们那里得到的信息。

经过进一步调查,结果如下。由于 Intellisense 的工作方式,我们无法支持涉及 aspx.cs 文件上共享代码隐藏的场景的 Intellisense。可以采取两种不同的方法来处理这种情况。 VS 中支持的选项是使用 AppCode 或 UserControll 作为公共代码元素,然后调用这些方法来实现公共代码库。第二个选项涉及以当前使用 CodeBehind 的方式使用它(不使用 Intellisense),并假设两个设计页面的代码都是正确的,代码应该可以正确编译,因为它是 ASP.NET 支持的方案。感谢您的反馈。

这意味着

  1. Intellisense 不适用于两个页面,而只能用于一页,
  2. 只有当两个控件都在两个页面上时,您的代码才会编译!

这确实违背了共享代码隐藏文件的想法。我的场景很可能是两个略有不同的页面,它们使用相同的代码。但对于 Microsoft 来说,两个略有不同的页面,不能使用相同的代码隐藏文件

理想场景应该是

  1. Intellisense 应该在两个页面中选择控件
  2. 如果所访问的控件存在于两个页面中的任何一个中,则代码应该编译。

Not truly supported by Microsoft

I discussed this with Microsoft and here is what I got from them.

After further investigation, here are the results. Due to the manner in which Intellisense works, we cannot support Intellisense for scenarios involving a shared code behind on the aspx.cs files. There are two different approaches that one can take to deal with this scenario. The option supported in VS is using either AppCode or UserControll for the common code elements and then calling those methods to achieve a common code base. The second option involves using the CodeBehind in the manner that you are currently using it (without Intellisense), and assuming that the code is correct with respect to both design pages, the code should compile correctly since it is an ASP.NET supported scenario. Thank you for your feedback.

So here what that means

  1. Intellisense will not work with both the pages, but only with one page
  2. your code will compile only if both the controls are on both the pages!

This really is against the idea of having a shared codeBehind file. My scenario will most likely be two slight different pages which uses same code behind. But for Microsoft, two slightly different pages, can not use the same codebehind file

Ideal Scenarios should be

  1. Intellisense should pick controls in both the pages
  2. Code should compile if the the control that is accessed is present in either of the two pages.
我是男神闪亮亮 2024-11-26 19:11:54

使用页面之间共享的类绝对没问题。它已经使用了一段时间了。然而,最好的方法是;如果您创建基类并继承该类而不是 System.Web.UI.Page:

请参阅以下链接:

  1. https://web.archive.org/web/20211020133935/https://www.4guysfromrolla.com/articles/041305-1.aspx
  2. http://aspalliance.com/63_Base_Page_and_User_Control_Classes

祝你好运

It is absolutely fine to use a class shared between pages. It has been in use for a while. However the best approach would be; if you create a base class and inherit that class instead of System.Web.UI.Page:

See the following links:

  1. https://web.archive.org/web/20211020133935/https://www.4guysfromrolla.com/articles/041305-1.aspx
  2. http://aspalliance.com/63_Base_Page_and_User_Control_Classes

Good Luck

初相遇 2024-11-26 19:11:54

类名 = CsForAllPages.cs
内容:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class general : System.Web.UI.Page
{
        // Your Same Code For all .aspx Pages
}

在接下来的aspx页面中设置

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="~/CsForAllPages.cs " Inherits="general" %>

Class Name = CsForAllPages.cs
Content :

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class general : System.Web.UI.Page
{
        // Your Same Code For all .aspx Pages
}

in the next in aspx pages set

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="~/CsForAllPages.cs " Inherits="general" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文