Export (0) Print
Expand All

Walkthrough: Debugging a Web Form

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.

NoteNote

To complete this walkthrough, you must have Administrator privileges on the server machine. By default, the ASP.NET process, aspnet_wp.exe or w3wp.exe, runs as an ASP.NET process. For more information, see ASP.NET Debugging: System Requirements. As a result, you must have Administrator privileges on the machine where ASP.NET runs to debug it.

First, you must close any open solutions.

NoteNote

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 prepare for this walkthrough

  • If you already have a solution open, close it.

    Next, you will create a new Web Form.

To create the Web Form

  1. On the File menu, click New, and then click Web Site.

    The New Web Site dialog box appears.

  2. In the Templates pane, click ASP.NET Web Site.

  3. On the Location line, click HTTP from the list, and in the text box, type http://localhost/WebSite.

  4. In the Language list box, click Visual C#, Visual J#, or Visual Basic.

  5. Click OK.

    Visual Studio creates a new project, and displays the default HTML source code. It also creates a new virtual directory named WebSite under IIS's Default Web Site.

  6. Click the Design tab on the bottom margin.

  7. Click the Toolbox tab on the left margin, or select it from the View menu.

    The Toolbox opens.

  8. In the Toolbox, click the Button control and drag the control to the main design surface, Default.aspx.

  9. In the Toolbox, click the Textbox control, and drag the control to the main design surface, Default.aspx.

  10. Double-click the button control you dropped.

    This takes you to the code page: Default.aspx.cs for C#, Default.aspx.vb for Visual Basic, or Default.aspx for J#. The cursor should be in the function Button1_Click.

  11. In the Button1_Click function, add the following code:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    TextBox1.Text = "Button was clicked!";
    
  12. On the Build menu, click Build Solution.

    The project should build with no errors.

    Now, you are ready to begin debugging.

To debug the Web Form

  1. In the Default.aspx.cs or Default.aspx.vb window, click the left margin on the same line as the text you added:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    textBox1.Text = "Button was clicked!";
    

    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.

  2. On the Debug menu, click Start.

  3. A 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.

  4. 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.

  5. On the Debug menu, click Windows, then click Watch, and then click Watch1.

  6. In the Watch window, type TextBox1.Text.

    The Watch window shows the value of the variable TextBox1.Text:

    ""
    
  7. On the Debug menu, click Step Into.

    The value of TextBox1.Text changes in the Watch window to read:

    "Button was clicked!"
    
  8. On the Debug menu, click Continue.

  9. In Internet Explorer, click the button again.

    Execution stops at the breakpoint again.

  10. In the Default.aspx.cs or Default.aspx.vb window, click the red dot in the left margin.

    This removes the breakpoint.

  11. On the Debug menu, click Stop Debugging.

To attach to the Web Form for debugging

  1. In the Default.aspx.cs or Default.aspx.vb window, click in the left margin to once again set a breakpoint at the line you added:

    ' Visual Basic
    TextBox1.Text = "Button was clicked!"
    
    // C#
    textBox1.Text = "Button was clicked!";
    
  2. On the Debug menu, click Start Without Debugging.

    The Web Form starts running under Internet Explorer, but the debugger is not attached.

  3. Attach to the ASP.NET process. For more information, see Debugging Deployed ASP.NET Web Applications/XML Web Services.

  4. 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.

  5. When you are finished debugging, on the Debug menu, click Stop Debugging.

Community Additions

ADD
Show:
© 2014 Microsoft