I'm just starting out learning ASP.NET. From what I understand, ASP.NET differs from old school ASP in that the logic code for a page exists in as separate file rather then being embedded in the ASP page. So when a user requests a page like ShoppingCart.aspx the server reads the directive at the top ...
<%@ Page Title="" Language="C#" MasterPageFile="~/Styles/Site.Master" AutoEventWireup="true"
CodeBehind="MyShoppingCart.aspx.cs" Inherits="TailspinSpyWorks.MyShoppingCart" %>
This tells the server what file and what class in the file is associated with the page. The code behind class also has member variables that correspond to each control on the page, and provide a way for code in the code behind file to manipulate the controls.
First, do I understand this correctly?
Second, could a site be setup with two separate ASPX pages with identically named controls, which both had a directive pointing to the same file and class? Would you even want to do this? Both pages could have the same functionality but a different layout. I was 开发者_JAVA百科thinking this could be a way to create separate "desktop" and "mobile" versions of a page with out duplicating content in the code behind files.
I guess ultimately what I'm wondering, is if there a way to define an abstract page? Say create an abstract page definition that says a page must have controls "cart_list", "total_lbl", but then be able to have multiple pages that inherit from this abstract page?
Yes, two pages can inherit from the same class. Like it can inherit from Page
class directly and do not even have an associated .cs file (useful when you have a page which is not static, but which does not handle events or something which may require a code-behind class).
In practice, I think it's not a good idea to inherit several ASP.NET pages from the same class directly. This is not something common, so:
- the code will be more difficult to understand and impossible to extend,
- will be difficult to manage within Visual Studio, especially when it comes to events, controls, etc.
- will cause much pain with existing/missing controls. See the detailed Guffa answer below.
If several pages of your website share the same logic,
- make one class per page, and inherit those classes from a common parent class which will contain common methods and properties and which will inherit from
Page
class. You will obtain an extensive and easy-to-understand solution. - or create a masterpage if the case is a good candidate for a masterpage.
I'm just starting out learning ASP.NET. From what I understand, ASP.NET differs from old school ASP in that the logic code for a page exists in as separate file rather then being embedded in the ASP page
Classic ASP and ASP.NET differ in a lot of ways, the primary way being that classic ASP pages are (for the most part) a procedural, script-based, unmanaged code whereas ASP.NET pages are compiled, managed, and event-driven. Typically ASP.NET pages separate out the markup and the server-side code into two separate files, but this isn't a necessity. It is quite possible to put the server-side code in the .aspx page in a <script runat="server"> block.
the directive at the top ...
<%@ Page Title="" Language="C#" MasterPageFile="~/Styles/Site.Master" AutoEventWireup="true" CodeBehind="MyShoppingCart.aspx.cs" Inherits="TailspinSpyWorks.MyShoppingCart" %>
tells the server what file and what class in the file is associated with the page. The code behind class also has member variables that correspond to each control on the page, and provide a way for code in the code behind file to manipulate the controls. First, do I understand this correctly?
Yes, you understand this correctly.
Second, could a site be setup with two separate ASPX pages with identically named controls, which both had a directive pointing to the same file and class?
Yes, you could do this.
Would you even want to do this?
Probably not. In fact, even if there are two separate ASPX pages that both inherit from the same base page there's nothing that forces them to have the same set of controls. In fact, they could have different controls and the page will render without error. (If you try to access a control in the code-behind that does not exist in one of the pages you'll get a runtime error.)
I guess ultimately what I'm wondering, is if there a way to define an abstract page? Say create an abstract page definition that says a page must have controls "cart_list", "total_lbl", but then be able to have multiple pages that inherit from this abstract page?
There's not (to my knowledge) a way to accomplish this. What might come close, though, is having a base Page class, which is a good practice to get use even if you don't have this particular scenario at hand. For more on creating and using base Page classes, see: Using a Custom Base Class for your ASP.NET Pages' Code-Behind Classes.
Happy Programming!
You might want to check this out, if you're using .NET 4.0. It describes Request.Browser.IsMobileDevice
and Request.Browser.MobileDeviceModel
.
You could put some logic into the code-behind, or the ASPX mark-up, to detect if you're running on a mobile device. That will allow you to have all the code in one file, but can select which HTML elements to display, etc.
Yes, and no.
You can use the same class for different pages, however the binding between the page controls and the variables in the class is not strict.
The control references will simply be assigned to the variables if they exist and have a matching type, but you can't make any restrictions that the page has to contain certain controls. You can however check if the variables has been assigned or if they contain null references the first you do in the code.
I have used the same class for different pages at some rare occasion, but it's not common practice. Usually there is little gain in doing this, and if you want to reuse code in the pages, you can put it in a class file and use it from the separate pages.
精彩评论