Thursday, April 23, 2015

ASP.Net C# How to create a simple debug panel within your page instead of using Response.Write

Here's a simple way to create a temporary debug panel within your ASP.Net web application if you need to output values for debugging and the normal debugging options aren't available or practical.

 <asp:PlaceHolder ID="pnlDebug" Visible="true" runat="server"></asp:PlaceHolder>  

 public void Debug(string Value = "")  
    pnlDebug.Controls.Add(new LiteralControl(string.Format("<div>{0}</div>", Value)));  


 <%@ MasterType VirtualPath="~/Base.master" %>  

 // debug  
 if (Configuration.Debug)  
    Debug(string.Format("SessionID: {0}", Configuration.SessionID));  
    Debug(string.Format("Language: {0}", Configuration.Language));  

The reason I like this approach is because it gives you the ability to avoid using Response.Write for debugging, which can mangle your layout while testing. It also allows you to build in optional debugging that you can turn on and off through the session and/or querystring (for example) to make debugging easier for applications where step-through debugging is not an option.