Telerik blogs
DotNetT2 Light_1200x303

Debugging is a crucial skill that can help any developer create better code more effectively. Check out these six tips for ways to improve your server-side debugging with ASP.NET.

Writing an ASP.NET application is not an easy task, but the real big challenge is to find out why that line of code designed to do something isn’t working as expected.  

Knowing how to debug your application is just as important as creating it. As a crucial developer skill, it is never too early or too late to up-skill your debugging knowledge and practice. 

In this article I would like to share six tips along with short videos that will make your life easier and help increase your efficiency.

Contents

Tip 1: Know Your Workspace and Tools

Visual Studio is a powerful application/IDE loaded with amazing tools we tend to ignore as learning them all could be overwhelming. I absolutely understand, since I, myself, avoided doing that until I realized that investing a little time on educating myself about the tools and preparing my workspace could pay off in the long run.  

I think every .NET developer should know the basics of debugging in Visual Studio, and the internet offers millions of articles, video and tutorials which make it very easy for us to learn.  

Perhaps you already have lots of experience with Visual Studio IDE, which is very good. It will be easier to learn about the Visual Studio Debugger. Here are some tutorials discussing that topic: 

Once you familiarize yourself with the Debugger, you can check out and learn about the power of Watch windows and QuickWatch.  

While Visual Studio delivers lots of powerful tools, learning the debugging part will put you on the next level! 

Tip 2: Identify the Types of Objects

After mastering the Debugging Tools, things will start to make more sense. You might remember the moments when you write a few lines of code and although the code seems fine, the app fails with Exceptions such as "System.NullReferenceException: Object reference not set to an instance of an object.", or "System.InvalidCastException: Specified cast is not valid". 

Searching on the internet for explanations/solutions can be extremely time consuming and overwhelming, but if you take advantage of the Developer Tools, place a breakpoint then run the application in debug mode, you will be able to find out the answers first hand.

The following Video will show you how to identify Objects/Classes using the QuickWatch in Visual Studio Debug mode:

Tip 3: Inspect the Immediate Children of a Control

Finding and accessing Controls inside others is the most wanted topic developers are seeking support for. Fortunately, the .NET Framework is a well-designed technology that provides us the tools and options to facilitate our work. 

The Control.Controls property will allow you to access the Control collection containing the immediate children of a Control. This is widely used in .NET Applications, but also very popular for understanding the Child controls of complex Telerik Controls such as the RadGrid in Telerik UI for ASP.NET AJAX

We have prepared a short video with an example that involves generic WebForms Controls and also shows an example using Telerik RadGrid. 

Tip 4: Find Out the Parents of a Control

In ASP.NET almost every component inherits from the Control class, and the structure of the WebForms pages is a complete Tree of Controls.

There is the Web Forms Page as a Control. The Page has the Form as a Child Control and the Form has its own.

With that in mind, the .NET Framework allows you to access them from Top to Bottom or the other way around. 

If you were to check the Parent of a Control, you could use the Control.Parent property. That will show you the immediate parent of the current Control. 

Check out the following short video demo showing an example with generic WebForms Controls and Telerik RadGrid. 

Tip 5: NamingContainers are smart Containers

NamingContainers are smart because they take care of one of the most important parts of the developing process. That is by eliminating the risk of having multiple controls with the same ID on the page. They come in handy especially when Controls are declared in the markup that will be duplicated and generating dynamic IDs is not an option.  

One example would be to place a Control inside a repeater which then repeats the Control multiple times, or placing the Control inside RadGrid. That will be multiplied as many times as many rows are in the Grid.

Like Control.Parent, the Control.NamingContainer property also returns a reference to one of the Parents of the Current control, but not necessarily the immediate Parent if that is not a NamingContainer. 

Here are two example when the Button is the Grand Child of a Naming container. In this case the button is inside a TableCell, and that inside a TableRow/GridDataItem and a reference of the GridDataItem is needed. 

Example using Control.Parent:

The immediate parent of the Button ID="btn" is the GridTableCell, and the Parent of the TableCell is the GridDataItem.

GridDataItem row = (btn.Parent as GridTableCell).Parent as GridDataItem;

Example using Control.NamingContainer:

The immediate parent of the Button ID="btn" is the GridTableCell, but the Grid row is needed which is a NamingContainer. Using the NamingContainer property will give you access directly.

GridDataItem row = btn.NamingContainer as GridDataItem;

Check out the following short video demonstration:

Tip 6: Find Controls by Their ID

In addition to checking a Control's children by using the Control.Controls property, you can also search for Controls by their ID using the Control.FindControl() method.

Here's an impractical example of finding the Button ID="Button1" by accessing the Control's children and moving down on the object tree.

Button btn = Panel1.Controls[0].Controls[3].Controls[2] as Button;

A more practical example would be to use the FindControl method to find the Button ID="Button1" by its ID.

Button btn = Panel1.FindControl("Button1") as Button;

Here is a short video demonstrating that:

Bonus Video: Accessing the Data Key Value of the Clicked Row in Telerik RadGrid

Below you can find a short video showing an example of using the GetDataKeyValue() method while inspecting the result in Visual Studio Debug mode and QuickWatch. 

Conclusion

You may already know a lot about ASP.NET and with a little bit of additional skills on debugging, you can raise that to a whole new level. These debugging tools are part of the .NET Framework and Visual Studio IDE, hence you can use them in every .NET Application. Be that WinForms, ASP.NET MVC, .NET Core or even WPF.

Remember, when you are working with complex components such as the RadGrid, RadPivotGrid, RadTreeList or RadAjaxManager/RadAjaxPanel, place a few breakpoints and start the debugger, and make this a habit of yours. You'll thank me later :). You can use that even if you only want to check some results when a simple RadButton is clicked.

If you would like to learn a few tips on debugging client-side apps (CSS, JavaScript, HTML) I would recommend checking out the Improve Your Debugging Skills with Chrome DevTools Blog Post which shares a few aspects of debugging an application on the client-side (in browser) using the browser’s built-in tools. 

You may also check out the Telerik UI for ASP.NET AJAX - Knowledge Base for various custom-scenarios we have created for our customers due to their high demand. 

Your Thoughts

Do you often troubleshoot or debug your application? If so, which approach do you take? If you'd like more advanced debugging tips, give us a sign and share your requests and ideas in the comments section below.


Attila Antal
About the Author

Attila Antal

Attila is a Senior Technical Support Engineer at Progress working with the Telerik AJAX, MVC and Kendo UI components. Prior to Progress, he worked as a Technical Trainer. Driven by his passion for technologies, he has experience with multiple scripting languages, but his main focus is on web development. In his free time he loves to hike, travel and do sport activities.

Related Posts

Comments

Comments are disabled in preview mode.