From the Debug menu, choose Start Debugging (or press F5) to run the page in debug mode.
If you have never run the debugger before, your application probably is not configured to support debugging. By default, debugging is turned off in applications both for performance (pages run more slowly in the debugger) and for security reasons. Visual Web Developer displays a message telling you what it must do to enabled debugging.
The switch to enable debugging is stored as a setting in the Web.config file, which maintains various site-specific configuration options. If the Web.config file does not exist, Visual Web Developer will both create the file and make the appropriate debugger setting.
If the Web.config file already exists but debugging is not enabled, you will see a slightly different message telling you that Visual Web Developer will modify the Web.config file.
If you see the message telling you that debugging has not been enabled, click OK to enable debugging.
In Visual Web Developer, the designer changes to debug mode displaying the code for your page and some debugger windows.
The debugger runs your page line by line. When the debugger gets to the line with the breakpoint, it stops and highlights the line.
Because the breakpoint is in the Page_Load handler, the page has not finished processing yet. The browser is open, but the page is not yet displayed.
In the Debug menu, click Windows, click Watch, and then click Watch 1.
Note: |
|---|
If you are using Visual Web Developer Express Edition, the debugger offers only a single
Watch window.
|
This opens a Watch window, where you can specify the values you want to track.
In the editor, right-click the IsPostBack portion of the Page.IsPostBack expression, and then click Add Watch.
This adds the expression to the Watch window and displays the current value of the property (false) is displayed in the Value column. If you prefer, you can type the name of a variable or property in the Name column of the Watch window.
From the Debug menu, choose Continue to continue execution, or press F5.
The Continue command tells the debugger to proceed until it gets to the next breakpoint. The Page_Load event handler finishes processing and the page is displayed in the browser.
Enter the value 2 into the text box and click the Square button.
The debugger is displayed again, with the breakpoint on the line in the Page_Load handler. This time, the Watch window shows you that the value of Page.IsPostBack is true.
Press F5 again to continue.
The debugger processes the Page_Load handler and enters the SquareButton_Click handler, where it stops on the second breakpoint you set.
In the Debug menu, click Windows and then click Locals.
This opens the Locals window, which displays the values of all variables and objects that are in scope at the current line being executed. The Locals window provides an alternative way for you to view these values, with the advantage that you do not have to explicitly set a watch on the elements, but with the disadvantage that the window might contain more information than you want to see at once.
In the Locals window, you see that the value of number is 2 and the value of result is 0.
Note: |
|---|
You can also see the value of any variable in the program by holding the mouse pointer over it.
|
In the Value column of the Locals window, right-click the line for the number variable and select Edit value. Edit the value of the number variable and change it to 5.
The value 2 for the variable number is not a good test of the program, because adding and squaring 2 both result in 4. Therefore, while the program is running, you can change the value of this variable.
From the Debug menu, choose Step Into to step into the Square function, or press F11.
The Step Into command causes the debugger to execute a line and then stop again.
Continue stepping by pressing F11 until you reach the following line of code.
ResultLabel.Text = CStr(number) & " squared is " & CStr(result)
ResultLabel.Text = NumberTextBox.Text +
" squared is " + result.ToString();
The debugger walks through your code line by line. When the debugger executes the Square function, you can use the Locals window to check the data passed to the function (number) and the return value of the function (Square).
In the Debug menu, click Windows and then Immediate.
The Immediate window allows you to execute commands. You can use the window to evaluate expressions (for example, to get the value of a property).
In the Immediate window, type the following expression and press Enter.
The question mark (?) is an operator in the Immediate window that evaluates the expression following it. In this example, you are evaluating the Text property of the NumberTextBox control on the page. You can evaluate any variable, object property, or expression that combine these, using the same syntax that you would use in code.
In the Immediate window, type the following and press Enter:
In addition to evaluating expressions, the Immediate window allows you to change variables or properties
Press F5 to continue running the program.
When the page appears, it displays the result of passing 5 to the Square function. In addition, the text in the text box has been changed to 5.