Visual Studio Web Applications—Code Editor
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| Visual Studio Articles |
| © 2006 M. MacDonald, M. Szpuszta |
The Code Editor
Many of Visual Studio’s most welcome enhancements 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 (see Figure 2-14).
![]()
Figure 2-14. Adding a web.config file automatically
Click OK to add this configuration file. Then, Visual Studio will launch your default browser, with the URL set to your page. 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 Figure 2-15).

Figure 2-15. Testing a simple web page
|

