Getting Started in Visual Studio .NET

Open Visual Studio .NET, and a start page will be displayed (unless it has been configured not to display the start page). Like the previous version of Vision Studio, a project template splash screen is provided by default, but this can be disabled. Let's create a new ASP.NET web forms project:

  1. Click the New Project button on the splash screen and the New Project window will open displaying project types and the corresponding project templates.

  2. Select the Visual C# Projects node in the left list. The project templates that correspond to C# will be displayed in the right list.

  3. Select the ASP.NET Web Application project template.

  4. Enter a project name other than the default name offered in the location text box. The server name may also be specified using the Uniform Resource Locator (URL), as shown in Figure 14-2, or using a Universal Naming Convention (UNC) file path to the web root. The default server name, localhost, could be specified in the URL if you wanted to host the solution on your workstation during the initial code and unit test phase. In this example, the name SimpleWF was chosen.

    click to expand
    Figure 14-2: Naming the SimpleWF ASP.NET web application project

  5. Click the OK button. Visual Studio .NET will connect to the hosting server and generate the project files. Visual Studio .NET alters the web instance on port 80 on the host when a new project is added by creating a virtual directory in the instance that maps to the file directory where the web files reside for the web forms project. If no IIS web instance appears on port 80 or if IIS is not running, Visual Studio .NET will fail to generate the web application project.

If Visual Studio .NET was able to successfully connect to the server specified in the New Project window, the following files will be generated by a default ASP.NET web application project:

  • AssemblyInfo.cs

  • Global.asax

  • Global.asax.cs

  • Global.asax.resx

  • SimpleWF.csproj

  • SimpleWF.csproj.webinfo

  • SimpleWF.vsdisco

  • Web.config

  • WebForm1.aspx

  • WebForm1.aspx.cs

  • WebForm1.aspx.resx

After Visual Studio .NET completes the creation of the web form application on the host, the editor will display a blank web form named WebForm1, which is prepared for editing in design view, as shown in Figure 14-3.

click to expand
Figure 14-3: Web form displayed in the Web Forms Designer design view

All web forms in Visual Studio .NET are edited using the Web Forms Designer. The Web Forms Designer lets you switch the web form to HTML view at any time to reveal the XML and HTML code that is in the ASPX file by clicking the HTML button at the lower-left corner of the web form window. Switching back to design view is easily accomplished by clicking the Design button next to the HTML button. The Web Forms Designer is smart enough to reflect changes made manually to the HTML view. The developer does not have to update the web form using the design view exclusively. The ASPX file may be edited using either view in the Web Forms Designer interchangeably.

Web Form File-ASPX

The ASPX file is the most visible file in an ASP.NET web application. The end user will request the ASPX file in their web browser to consume all of the programmatic functionality that supports the ASP.NET web application. If an end user wants to view the web form shown in that file from IIS, he or she would request http://<myhostURL>/ <my virtual web directory>/WebForm1.aspx in the web browser's address field.

The virtual web directory created is the same name as the project, by default. All ASP.NET web applications generate a virtual web directory when they are deployed to IIS. The files WebForm1.aspx.cs and WebForm1.aspx.resx are used to produce the project DLL or assembly that will result from the WebForm1.aspx being compiled. The aspx.cs could be thought of as the implementation code file of the ASPX file, and the ASPX file could be thought of as a header file of sorts. The .aspx.cs file contains all the code behind the ASPX file and the aspx.resx is the resource file for the web form.

Listing 14-1 shows the HTML view of an ASPX file for an ASP.NET web form file. You can edit the HTML and client side javascript in the ASPX file using the HTML view, but by using the ASP.NET web form controls in the design view, you can build a sophisticated web page without writing any HTML code. The design view of the Web Forms Designer in Visual Studio .NET is a 'what you see is what you get' (WSYWIG) editor that abstracts you from the HTML code and provides a medium for building web pages that works like an Integrated Development Environment (IDE) for desktop applications like Visual Basic 6. If you are more comfortable coding the ASPX file in the HTML view, you can use the HTML section of the toolbox to get HTML elements.

Listing 14-1: HTML View for requestSomething.aspx

start example
 <%@ Page language="c#" Codebehind="requestSomething.aspx.cs"  AutoEventWireup="false" Inherits="SimpleWF.requestSomething" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>   <HEAD>    <title>requestSomething</title>   <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">   <meta name="CODE_LANGUAGE" Content="C#">   <meta name="vs_defaultClientScript" content="JavaScript">   <meta name="vs_targetSchema" content="http://schemas. microsoft.com/intellisense/ie5">   </HEAD>   <body MS_POSITIONING="GridLayout">    <form  method="post" runat="server">     <asp:TextBox  style=      "Z-INDEX: 101; LEFT: 193px; POSITION: absolute;       TOP: 46px" runat="server" Width="330px"       Height="25px">     </asp:TextBox>     <asp:Label  style=      "Z-INDEX: 102; LEFT: 26px; POSITION: absolute;        TOP: 41px" runat="server" Width="155px"        Height="43px">       Tell me about yourself and click submit:     </asp:Label>     <asp:TextBox  style=      "Z-INDEX: 103; LEFT: 57px; POSITION: absolute;        TOP: 148px" runat="server" Width="435px"        Height="101px" ReadOnly="True"        TextMode="MultiLine">     </asp:TextBox>     <asp:Button  style=      "Z-INDEX: 104; LEFT: 298px; POSITION: absolute;        TOP: 79px" runat="server" Width="131px"        Height="43px" Text="Submit">     </asp:Button>     <asp:Label  style=      "Z-INDEX: 105; LEFT: 66px; POSITION: absolute;       TOP: 116px" runat="server" Width="138px"       Height="27px">You Said:     </asp:Label>    </form>   </body> </HTML>
end example

The first line of requestSomething.aspx identifies the Codebehind file requestSomething.aspx.cs. The Global.asax uses the same filename convention as the requestSomething.aspx file by referencing a Codebehind file called Global.asax.cs. The Global.asax file serves the same purpose that it did in legacy ASP-it provides a file location where code may be written, which is accessible by all the web forms in the given project and allows the developer to capture application-specific events. For example, just as the Global.asa file in ASP provides events for application start and end and session start and end, the Global.asax file provides a class named Global that inherits System.Web.HttpApplication and has member functions that provide the same events. As an added benefit, Global.asax also provides new events that capture the start and end of a given resource request, authentication, and application errors.

The web form requestSomething.aspx includes three web form controls. The web form controls were added in design view using the Web Forms part of the toolbox. The Web Forms Designer in Visual Studio .NET offer developers a unique opportunity to work with web forms as though they were writing code for desktop applications. The toolbox in Visual Studio .NET allows developers to paste controls onto the web form just as if they were creating a desktop application form in Visual Basic or Visual C++. The interfaces to the controls are almost identical to the control counterparts found in desktop software.

Developers can write code behind the web forms in a similar way that code was written behind form classes in the Visual Basic or Visual C++ desktop development environment. The properties view for each control is formatted into XML and placed in the ASPX file. The .NET Framework uses the XML to initialize the respective control and produce HTML that is eventually returned to the consumer. The Visual Studio .NET toolbox also provides developers a selection of HTML elements. For example, both the DataGrid control and the HTML table element will produce results that display a table in the web browser. The web forms DataGrid control provides greater programmatic control of the population and configuration of the table versus the HTML table. Listings 14-2 and 14-3 illustrate the differences in the ASPX file between the DataGrid web form control and the HTML element. Listing 14-2 shows the XML placed in the ASPX file for a DataGrid control.

Listing 14-2: <asp:DataGrid

start example
style="Z-INDEX: 104; LEFT: 88px; POSITION: absolute; TOP: 258px" runat="server"> </asp:DataGrid>
end example

Listing 14-3 shows an HTML element created when you paste a table from the HTML part of the toolbox onto the web form.

Listing 14-3: <TABLE style="Z-INDEX: 103; LEFT: 191px; POSITION: absolute; TOP: 291px" cellSpacing="1" cellPadding="1" width="300" border="1">

start example
  <TR>    <TD></TD>    <TD></TD>    <TD></TD>   </TR>   <TR>    <TD></TD>    <TD></TD>    <TD></TD>   </TR>   <TR>    <TD></TD>    <TD></TD>    <TD></TD>   </TR> </TABLE>
end example

In the overly simple web form requestSomething.aspx, shown in Figure 14-4, the end user can type in some personal information and click the Submit button. After the web form is submitted, the form responds to the end user with the contents typed in the text box labeled Tell Me About Yourself And Click Submit and also typed in the You Said text box. Figure 14-4 shows the requestSomething.aspx form after some information has been submitted to the server.

click to expand
Figure 14-4: Web form requestSomething.aspx in action

Web Form Codebehind File-aspx.cs

Using classic ASP, the developer places code in the ASP file to extract the posted data typed into a text box after the end user clicks the Submit button. After the data is extracted from the Request object, the data is placed in the value attribute for the other text box that displays what the end user submitted. The following line of code can achieve the result in an ASP file:

<textarea name="txtResponse" ><% =Request("txtYourself") %> </textarea>

The ASP.NET web form achieves the result with more code but with greater organization and better form than the ASP. Listing 14-4 shows the code behind the web form requestSomething.aspx in the Codebehind file requestSomething.aspx.cs. The Visual Studio .NET Web Forms Designer automatically generates the majority of the code shown in Listing 14-4. If you open the file requestSomething.aspx in the design view and double- click the control named txtYourself, which is the top text box control, the Web Forms Designer will create an event in the Codebehind file requestSomething.aspx.cs named txtYourself_TextChanged. The Web Forms Designer will open the file requestSomething.aspx.cs to the event function created for the web form control txtYourself. Although the Web Forms Designer may generate much of the code within the Codebehind file, you can edit code within the file as needed because the Codebehind file is the intended location for the supporting code for the ASPX file.

Listing 14-4: Source Code for the requestSomething.aspx CodeBehind File requestSomething.aspx.cs

start example
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace SimpleWF {   /// <summary>   /// Summary description for requestSomething.   /// </summary>   public class requestSomething : System.Web.UI.Page   {    protected System.Web.UI.WebControls.Button Button1;    protected System.Web.UI.WebControls.TextBox txtYourself;    protected System.Web.UI.WebControls.TextBox txtResponse;    protected System.Web.UI.WebControls.Label Label2;    protected System.Web.UI.WebControls.Label Label1;       private void Page_Load(object sender, System.EventArgs e)    {     // Put user code to initialize the page here    }    #region Web Form Designer generated code    override protected void OnInit(EventArgs e)    {     //     // CODEGEN: This call is required by the      // ASP.NET Web Form Designer.     //     InitializeComponent();     base.OnInit(e);    }        /// <summary>    /// Required method for Designer support - do not modify    /// the contents of this method with the code editor.    /// </summary>    private void InitializeComponent()    {      this.txtYourself.TextChanged += new     System.EventHandler(this.txtYourself_TextChanged);     this.Load += new      System.EventHandler(this.Page_Load);    }    #endregion    private void txtYourself_TextChanged        (object sender, System.EventArgs e)    {     this.txtResponse.Text = this.txtYourself.Text;    }   } }
end example

The event handler for the web form is called when the value in txtYourself is changed and submitted in a form to the web server. When the form submission is made to the server, the code in the Codebehind file for the web form requestSomething.aspx determines that a value was submitted for the text box named txtYourself and calls the event function txtYourself_TextChanged. In this particular event, one line of code was actually written to place the value submitted into the text box txtResponse:

this.txtResponse.Text = this.txtYourself.Text;

Both text boxes are treated like controls or class instances in a normal software application, abstracting the programmer entirely from the details of the HTML. Because the programmer can paste controls in the design view and edit code that relates to the behavior of the control after clicking the specific control, the programmer is almost completely removed from ever having to edit in HTML.

AssemblyInfo.cs File

The ASP.NET project that Visual Studio .NET created, named SimpleWF, results in an assembly being created. The assembly is the deployment package for the web form files that make up the project, which is simply a DLL. When an ASPX file is requested for the first time from IIS, the assembly is produced. The Codebehind file and the resource file that are referenced in the ASPX file are compiled in Microsoft instruction language (MISL), which has a similar format to assembly code not unlike binary products of other conventional compilers. The MISL is produced only for the code subroutines that are executed. The MISL will not be produced for functions or subroutines that were not executed.

The files AssemblyInfo.cs, SimpleWF.csproj, SimpleWF.csproj.webinfo, and SimpleWF.vsdisco all provide information for the compiler to produce the assembly that hosts the MISL generated from the ASP.NET files. The AssemblyInfo.cs file contains information that describes the assembly, such as version and name. You can also use the file to sign your assemblies by assigning a key. Assembly signing provides a mechanism that will protect your assembly from being reverse-engineered. Listing 14-5 shows a blank AssemblyInfo.cs file. You can edit the assembly file directly, and you do not have to be comprehensive. You can edit the values that are known and leave other parts blank.

Listing 14-5: Blank AssemblyInfo.cs file

start example
 using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")]
end example

Project File-csproj

The csproj file is an XML file that contains information about the dependencies of the project and some compilation settings for the creation of the assembly. Visual Studio .NET updates this file as web forms, and references are added or removed from the project. You should not edit the csproj file in the normal course of working on an ASP.NET project. If you do open it however, you will see a file that describes all of the settings and elements that contribute to the project, such as filenames, deployment settings, and assembly references.

Project WebInfo File-csproj.webinfo

The csproj.webinfo file is an XML file that describes where the project file is located or hosted. Listing 14-6 shows the SimpleWF.csproj.webinfo file contents. This file is also generated and updated by Visual Studio .NET, and you should not edit it directly.

Listing 14-6: Contents of File SimpleWF.csproj.webinfo

start example
 <VisualStudioUNCWeb>  <Web URLPath = "http://amd1700/SimpleWF/SimpleWF.csproj" /> </VisualStudioUNCWeb>
end example

Discovery Information File-vsdisco

The vsdisco file identifies searchable paths on a development web server to enable ASP.NET to seek and find ASP.NET web services. The SimpleWF.vsdisco file is the default file generated for the project SimpleWF. Listing 14-7 shows the source of SimpleWF.vsdisco. You should edit the vsdisco file to help ASP.NET find other assemblies in a development environment by setting web directories that should not be searched. The default version of the vsdsico excludes the FrontPage extension directories because an assembly will not be located within any of those directories under normal circumstances.

Listing 14-7: Source of SimpleWF.vsdisco

start example
 <?xml version="1.0" encoding="utf-8" ?> <dynamicDiscovery xmlns=      "urn:schemas-dynamicdiscovery:disco.2000-03-17">   <exclude path="_vti_cnf" />   <exclude path="_vti_pvt" />   <exclude path="_vti_log" />   <exclude path="_vti_script" />   <exclude path="_vti_txt" />   <exclude path="Web References" /> </dynamicDiscovery>
end example




IIS 6(c) The Complete Reference
IIS 6: The Complete Reference
ISBN: 0072224959
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net