Microsoft.NET

……………………………………………….Expertise in .NET Technologies

The Code Editor

Posted by Ravi Varma Thumati on February 11, 2015

Many of Visual Studio’s handiest features appear when you start to write the code that supports your user interface. To start coding, you need to switch to the code-behind view. To switch back and forth, you can use two buttons that are placed just above the Solution Explorer window. The tooltips identify these buttons as View Code and View Designer. When you switch to code view, you’ll see the page class for your web page. You’ll learn more about code-behind later in this chapter.

ASP.NET is event-driven, and everything in your web-page code takes place in response to an event. To create a simple event handler for the Button.Click event, double-click the button in design view. Here’s a simple example that displays the current date and time in a label:

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = “Current time: ” + DateTime.Now.ToLongTimeString();

}

To test this page, select Debug ➤ Start Debugging from the menu. Because this is the first time running any page in this application, Visual Studio will inform you that you need a configuration file that specifically enables debugging, and will offer to change your current web.config file accordingly.

vsce1

Modifying a web.config file automatically

Click OK to change the web.config configuration file. Visual Studio will then start the integrated test web server and launch your default browser with the URL set to the current page that’s open in Visual Studio. At this point, your request will be passed to ASP.NET, which will compile the page and execute it.

To test your event-handling logic, click the button on the page. The page will then be submitted to ASP.NET, which will run your event-handling code and return a new HTML page with the data as shown in below Figure.

vsce2

Testing a simple web page

Adding Assembly References

By default, ASP.NET makes a small set of commonly used .NET assemblies available to all web pages. These assemblies (listed in Table) are configured through a special machine-wide configuration file. You don’t need to take any extra steps to use the classes in these assemblies.

Assembly Description
mscorlib.dll and System.dll Includes the core set of .NET data types, common exception types, and numerous other fundamental building blocks.
System.Configuration.dll Includes classes for reading and writing configuration information in the web.config file, including your custom settings.
System.Data.dll Includes the data container classes for ADO.NET, along with the SQL Server data provider.
System.Drawing.dll Includes classes representing colours, fonts, and shapes. Also includes the GDI+ drawing logic you need to build graphics on the fly.
System.Web.dll Includes the core ASP.NET classes, including classes for building web forms, managing state, handling security, and much more.
System.Web.Services.dll Includes classes for building web services—units of code that can be remotely invoked over HTTP.
System.Xml.dll Includes .NET classes for reading, writing, searching, transforming, and validating XML.
System.EnterpriseServices.dll Includes .NET classes for COM+ services such as transactions.
System.Web.Mobile.dll Includes .NET classes for the mobile web controls, which are targeted for small devices such as web-enabled cell phones.

 

Table Core Assemblies for ASP.NET Pages

If you want to use additional features or a third-party component, you may need to import more assemblies. For example, if you want to use an Oracle database, you need to add a reference to the System.Data.OracleClient.dll assembly. To add a reference, select Website ➤ Add Reference (or Project ➤ Add Reference in a web project). The Add Reference dialog box will appear, with a list of registered .NET assemblies (see below Figure).

In the Add Reference dialog box, select the component you want to use. If you want to use a component that isn’t listed here, you’ll need to click the Browse tab and select the DLL file from the appropriate directory (or from another project in the same solution, using the Projects tab).

If you’re working with a projectless website and you add a reference to another assembly, Visual Studio modifies the web.config file to indicate the assembly you’re using. Here’s an example of what you might see after you add a reference to the System.Data.OracleClient.dll file:

<compilation debug=”true”>

<assemblies>

           <add assembly=”System.Data.OracleClient, Version=2.0.0.0, …” />

</assemblies>

</compilation>

When you create an ASP.NET application that targets .NET 3.5, Visual Studio adds a small set of references automatically. These references point to a few assemblies that implement new features.

vsce3

Figure Adding a reference

If you’re working with a web project, and you add a reference to another assembly, Visual Studio doesn’t need to change the web.config file. That’s because Visual Studio is responsible for compiling the code in a web project, not ASP.NET. Instead, Visual Studio makes a note of this reference in the .csproj project file. The reference also appears in the Solution Explorer window under the References node. You can review your references here, and remove any one by right-clicking it and choosing Remove.

If you add a reference to an assembly that isn’t stored in the GAC (global assembly cache), Visual Studio will create a Bin subdirectory in your web application and copy the DLL into that directory. (This happens regardless of whether you’re using project-based or projectless development.)

This step isn’t required for assemblies in the GAC because they are shared with all the .NET applications on the computer.

If you look at the code for a web-page class, you’ll notice that Visual Studio imports a lengthy number of core .NET namespaces. Here’s the code you’ll see:

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

Adding a reference isn’t the same as importing the namespace with the using statement. The using statement allows you to use the classes in a namespace without typing the long, fully qualified class names. However, if you’re missing a reference, it doesn’t matter what using statements you include—the classes won’t be available. For example, if you import the System.Web.UI namespace, you can write Page instead of System.Web.UI.Page in your code. But if you haven’t added a reference to the System.Web.dll assembly that contains these classes, you still won’t be able to access the classes in the System.Web.UI namespace.

IntelliSense and Outlining

As you program with Visual Studio, you’ll become familiar with its many time-saving conveniences. The following sections outline the most important features you’ll use (none of which is new in Visual Studio 2008).

Outlining

Outlining allows Visual Studio to “collapse” a subroutine, block structure, or region to a single line. It allows you to see the code that interests you, while hiding unimportant code. To collapse a portion of code, click the minus box next to the first line. Click the box again (which will now have a plus symbol) to expand it (see Figure).

vsce4

Figure Collapsing code

You can collapse an entire code file so that it only shows definitions (such as the namespace and class declarations, member variables and properties, method declarations, and so on), but hides all other details (such as the code inside your methods and your namespace imports). To get this top-level view of your code, right-click anywhere in the code window and choose Outlining ➤ Collapse to Definitions. To remove your outlining and expand all collapsed regions so you can see everything at once, right-click in the code window and choose Outlining ➤ Stop Outlining.

Member List

Visual Studio makes it easy for you to interact with controls and classes. When you type a period (.) after a class or object name, Visual Studio pops up a list of available properties and methods (see Figure below). It uses a similar trick to provide a list of data types when you define a variable and to provide a list of valid values when you assign a value to an enumeration.

 vsce5

Figure. IntelliSense at work

Visual Studio also provides a list of parameters and their data types when you call a method or invoke a constructor. This information is presented in a tooltip below the code and is shown as you type. Because the .NET class library heavily uses function overloading, these methods may have multiple different versions. When they do, Visual Studio indicates the number of versions and allows you to see the method definitions for each one by clicking the small up and down arrows in the tooltip. Each time you click the arrow, the tooltip displays a different version of the overloaded method.

vsce6

Figure IntelliSense with overloaded method

Error Underlining

One of the code editor’s most useful features is error underlining. Visual Studio is able to detect a variety of error conditions, such as undefined variables, properties, or methods; invalid data type conversions; and missing code elements. Rather than stopping you to alert you that a problem exists, the Visual Studio editor quietly underlines the offending code. You can hover your mouse over an underlined error to see a brief tooltip description of the problem (see Figure below).

vsce7

Figure. Highlighting errors at design time

Visual Studio won’t flag your errors immediately. Instead, it will quickly scan through your code as soon as you try to compile it and mark all the errors it finds. If your code contains at least one error, Visual Studio will ask you whether it should continue. At this point, you’ll almost always decide to cancel the operation and fix the problems Visual Studio has reported. (If you choose to continue, you’ll actually wind up using the last compiled version of your application because the .NET compilers can’t build an application that has errors.)

Note: You may find that as you fix errors and rebuild your project you discover more problems. That’s because Visual Studio doesn’t check for all types of errors at once. When you try to compile your application, Visual Studio scans for basic problems such as unrecognized class names. If these problems exist, they can easily mask other errors. On the other hand, if your code passes this basic level of inspection, Visual Studio checks for more subtle problems such as trying to use an unassigned variable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: