Search code examples
.netasp.netcode-reuse

Two .aspx pages using the same .cs file, good idea?


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.


Solution

  • 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.