ASP.NET Web Site Debugging Part 7 Call Stack
This tutorial is the seventh part of the ASP.NET Web Site Debugging tutorial series. This tutorial will demonstrate how to use the Call Stack in Microsoft Visual Studio 2010 with ASP.NET 4.0 and C#.
The call stack is a stack that contains all of the methods that have been called to get to the current point in the program. The call stack window allows us to evaluate what methods have been called during runtime in Microsoft Visual Studio.
Creating a Web Site
To demonstrate the call stack window we will need to create a simple website with a few methods in it. At this point in the tutorial I have created a new ASP.NET Empty Web Site and am ready to add in some content and C# code. To begin:
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a label onto the web form.
Next, we need to add some simple code to call a couple of methods so that we can evaluate the call stack. To do this, add the following code to the Default.aspx.cs code behind file:
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
private void SetLabel1()
private void SetLabel2()
Label1.Text = "Hello Label from method 1";
Let’s review what this code actually does. First, we add in two methods, SetLabel1() and SetLabel2(). From our Page_Load event method we call SetLabel1() which then makes a call to SetLabel2(). Finally, SetLabel2() simply changes the text value of the label we added earlier.
Using the Call Stack Window
Now that we have some code to work with, let’s go ahead and debug it. First, we need to add in a breakpoint in the Page_Load event method at the line ‘SetLabel1();’. Then, go ahead and load up the web site with debugging and:
- From the top menu select Debug -> Windows -> Call Stack to view the call stack.
- Notice that the call stack contains some information and displays the Page_Load event method’s signature and line number. This is because it is currently the only call on our call stack.
- Hit F11 to step into our SetLabel1() method that we are currently at. Notice that SetLabel1() is added to the top of the call stack.
- Hit F11 twice to step into our SetLabel2() method. Notice that it is now added to the top of the stack.
- Hit F11 three more times to finish up our SetLabel2() method. Notice that it is removed from the call stack.
The call stack allows us to evaluate the current method calls that have been initiated to get to a certain point in your code. This can be helpful when you have method calls that are extremely deep within your code. Furthermore, you can determine what data you want to have the call stack display by right clicking in the call stack window and selecting different paremeters.