|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Walkthrough: Debugging a Web Form
This topic applies to:
Pro and Team
Does not apply
Command or commands hidden by default.
The steps in this walkthrough show you how to debug an ASP.NET Web application, also known as a Web Form. It shows you how to start and stop execution, set breakpoints, and examine variables in the Watch window.
To complete this walkthrough, you must have Administrator privileges on the server computer. By default, the ASP.NET process, aspnet_wp.exe or w3wp.exe, runs as an ASP.NET process. To debug ASP.NET, you must have Administrator privileges on the computer where ASP.NET runs it. For more information, see ASP.NET Debugging: System Requirements.
The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
To create the Web Form
If you already have an solution open, close it.
On the File menu, click New, and then click Web Site.
The New Web Site dialog box appears.
In the Templates pane, click ASP.NET Web Site.
On the Location line, click HTTP from the list, and in the text box, type http://localhost/WebSite.
In the Language list, click Visual C# or Visual Basic.
Visual Studio creates a new project, and displays the default HTML source code. It also creates a new virtual directory named WebSite under Default Web Site in IIS.
Click the Design tab on the bottom margin.
Click the Toolbox tab on the left margin, or select it on the View menu.
The Toolbox opens.
In the Toolbox, click the Button control and add it to the main design surface, Default.aspx.
In the Toolbox, click the Textbox control, and drag the control to the main design surface, Default.aspx.
Double-click the button control you dropped.
This takes you to the code page: Default.aspx.cs for C# or Default.aspx.vb for Visual Basic. The cursor should be in the function Button1_Click.
In the Button1_Click function, add the following code:
' Visual Basic TextBox1.Text = "Button was clicked!" // C# TextBox1.Text = "Button was clicked!";
On the Build menu, click Build Solution.
The project should build without errors.
Now, you are ready to start debugging.
To debug the Web Form
In the Default.aspx.cs or Default.aspx.vb window, click the left margin on the same line as the text you added:
A red dot appears and the text on the line is highlighted in red. The red dot represents a breakpoint. When you run the application under the debugger, the debugger will break execution at that location when the code is hit. You can then view the state of your application and debug it. For more information, see Breakpoints.
On the Debug menu, click Start Debugging.
The Debugging Not Enabled dialog box appears. Select Add a new Web.config file with debugging enabled option, and click OK.
Internet Explorer starts and displays the page that you just designed.
In Internet Explorer, click the button.
In Visual Studio, this takes you to the line where you set your breakpoint on the code page Default.aspx.cs or Default.aspx.vb. This line should be highlighted in yellow. You can now view the variables in your application and control its execution. Your application stops executing and waits for a command from you.
On the Debug menu, click Windows, then click Watch, and then click Watch1.
In the Watch window, type TextBox1.Text.
The Watch window shows the value of the variable TextBox1.Text:
On the Debug menu, click Step Over.
The value of TextBox1.Text changes in the Watch window to read:
"Button was clicked!"
On the Debug menu, click Continue.
In Internet Explorer, click the button again.
Execution stops at the breakpoint again.
In the Default.aspx.cs or Default.aspx.vb window, click the red dot in the left margin.
This removes the breakpoint.
On the Debug menu, click Stop Debugging.
To attach to the Web Form for debugging
In Visual Studio, you can attach the debugger to a running process. For most effective debugging, compile the executable as a Debug version with symbol (PDB) files.
In the Default.aspx.cs or Default.aspx.vb window, click in the left margin to again set a breakpoint at the line you added:
On the Debug menu, click Start Without Debugging.
The Web Form starts to run under Internet Explorer, but the debugger is not attached.
Attach to the ASP.NET process. For more information, see Debugging Deployed Web Applications and Web Services.
In Internet Explorer, click the button on your form.
In Visual Studio, you should hit the breakpoint in Default.aspx.cs, Default.aspx.vb, or Default.aspx.
When you are finished debugging, on the Debug menu, click Stop Debugging.