IBuySpy Portal Documentation | |
Visual Studio Project Notes (C#)Overview. This version of the IBuySpy Portal application has been packaged as a Visual Studio .NET application, specifically a Visual C# .NET "ASP.NET Web Application" project. In addition to showing the IBuySpy Portal application, this sample shows how to implement a real web application in VS.NET. Code-Behind. One of the big benefits of ASP.NET is a much more complete separation between static content (HTML, text and tags) from code. There are a couple of different ways to achieve this in ASP.NET. The SDK versions of IBuySpy show the "single-file" approach, where code is in the same file as the static content, but segregated into a single code block. VS.NET, on the other hand, favors ASP.NET's code-behind model for separating code from content. This way, the page code is a fully encapsulated class which gets the full benefits of code editing in VS.NET: syntax color coding, statement completion, auto-insertion of event handling methods, design-time syntax checking and full debugging support.
When you create a new ASP.NET page, or "Web Form," VS.NET creates both the .ASPX file, and the code-behind file. By default the code-behind file is hidden, but you can see it by selecting Project > Show All Files. To work with a Web Form in VS.NET, double-click the file name in Solution Explorer, or right-click and select one of the available actions. Open will open an ASPX file in design view, and a .CS file in code view. Open With permits you to select a different editor for the file, for example, Notepad. View Code always opens the code-behind for the selected file (even when the ASPX file is selected). Likewise, View Designer always opens the ASPX file.
The Web Forms designer also uses the <%@ Page %> directive (or <%@ Control %> directive for user controls) to associate the two files. Minimally, code-behind ASP.NET pages and user controls created in VS.NET need the following attributes: <%@ Page Language="c#" AutoEventWireup="false" Codebehind="WebForm1.aspx.cs" Inherits="PortalVS.WebForm1"%> <%@ Control Language="c#" AutoEventWireup="false" Codebehind="myControl.ascx.cs" Inherits="PortalVS.myControl"%>
Project Properties. In a Visual C# application, the .NET namespace for the code in the project is provided by the Project Properties, as is the name of the assembly to build. In this project, the assembly name is set to "Portal." The Default namespace is not set, which permits the developer to choose the namespace explicitly on a per-file basis. To see this, examine Project > Properties. Another interesting feature of Project Properties is the ability to specific Imports statements at the project level. This reduces the code in each code file by several lines. Compiling and Running the Application. Changes to the code-behind class must be compiled prior to running the page. You can compile using the Build > Build command.
To browse the application in a browser, right-click the Default.aspx file, and select one of the Browse options. View in Browser (Alt-V) will open the page in the default browser within VS.NET. Browse With... lets you select an external browser, and even set it as your "View in Browser" default. To launch the application in the debugger, first set your breakpoints. Then right click an appropriate page and select Set As Start Page. Then you can press F5 to launch the debugger. |