How to debug a user control?

utilitaire

Regular
Joined
May 4, 2005
Messages
77
I'm working with VS.net. I know how to debug code-behind pages, but I dont know how to debug a user control. A User-control has to be compiled and put into the bin directory before using it in a webform. But if the user-control has a code-behind page, and if I want to debug this part of the code. How do I do that???

page.aspx:

<%@ Register TagPrefix="val" Tagname="messageErreur" Src="controls/validators/messageErreur.ascx" %>

<val:messageErreur id="valSum" runat="server">
</val:messageErreur>

messageErreur.ascx contains a code-behind page that I want to debug with VS.net.

I would appreciated your help a lot. That's the most important part of VS.net that I dont understand.
 
With aspx you can output to the debug console using debug.write/writeline as you would using the console commands. Just remember to import the right namespace as below

import system.diagnostics


Thanks

Andy
 
thank you for your answer.

I'm trying this in 5 minutes.

But is there any way to debug a user-control in a classic way, With break points, F11, F12 ???
 
You can add break points in using the editor by clicking on the far left hand side of the line of code you wish to add a break point a small stop symbol will then be shown.

Hope it helps

Andy
 
yes a can to that with webform(code-behind). But I can't do that with user-control(code-behind).

When I create a webform, I hit F5 and I enter in debug mode. I can use break points.

But when a create a user-control, with code behind, I have to compile it first, right? I use a command line(is there a way to do that with VS.net?) to create the DLL file, and put it in the bin directory. After that, I can use this user-control in my webform:

<%@ Register TagPrefix="val" Tagname="messageErreur" Src="controls/validators/messageErreur.ascx" %>.

But since this code is compiled, I cant find the way to go into debug mode. Is there a way to debug a DLL with break points?????? I just cant understand how.

By the way, all my user-control classes are not in my VS projects. They're in a separated directory. I compile all these classes in DOS. Is this the right way to do that???
 
While I debug my usercontrols I tend to compile them all within the master project as usercontrol classes this enables me to put break points in this allows me to have a lot more control. Are you using VS2002/20003 or are you using a editor such as webmatrix?

Thanks

Andy
 
Ok I didn't know I could do that.

Actually, I'm really working with VS.net. But when I tried to create a user-control within VS.net, I created a tricky situation. First of all, when I hit «build project», VS.net does not create the DLL file from the user-control class. Therefor, I get a file not found error in the webform, when attempting to access my user-control: <%@ Register TagPrefix="eval" Assembly="vooral.controls.validators" Namespace="vooral.controls.validators" %>.

My user-control is an email validator. I want to use it in my webform. Here's the code.


using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace vooral.controls.validators
{
public class emailValidator: BaseValidator
{
private const string REGULAR_EXPRESSION = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
protected override bool EvaluateIsValid()
{
string val = this.GetControlValidationValue(this.ControlToValidate);
try
{
return Regex.IsMatch(val, REGULAR_EXPRESSION);
}
catch
{
return false;
}
}
}
}
 
You can make it work by the following first heres you control class

Code:
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace WebApplication1
{
	public class emailValidator: BaseValidator
	{
		private const string REGULAR_EXPRESSION = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
		protected override bool EvaluateIsValid()
		{
			string val = this.GetControlValidationValue(this.ControlToValidate);
			try
			{
				return Regex.IsMatch(val, REGULAR_EXPRESSION);
			}
			catch
			{
				return false;
			}
		}
	}
}

The only difference I have made is that i've changed the namespace to the same as the web applications, just for ease.

Now for registering the control on the page

Code:
<%@ Register TagPrefix="emailValidator" Assembly="WebApplication1"  Namespace="WebApplication1" %>

Nice and simple just register the control in the same namespace as the application the control then is part of the main application DLL.

This will the compile and run fine declaring the controls like

Code:
<emailValidator:emailValidator id="RegularExpressionValidator1" runat="server"  ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1"></emailValidator:emailValidator>

I've attached a webapplication to show

Hope this helps

Andy
 

Attachments

wow! Thank you for this very detailed answer! Very appreciated!

I tried to debug the project. When I run the project, the webform show this error:

http://www.vooral.com/WebApplication1/WebForm1.aspx

That's the error I get from the begining. I have never been able to debug a user-control because of that. This error is fired because IIS cant find the DLL in the Bin directory. Here's why I dont know how to debug a user-control, since it has to be put first in the bin directory.
 
utilitaire said:
wow! Thank you for this very detailed answer! Very appreciated!

I tried to debug the project. When I run the project, the webform show this error:

http://www.vooral.com/WebApplication1/WebForm1.aspx

That's the error I get from the begining. I have never been able to debug a user-control because of that. This error is fired because IIS cant find the DLL in the Bin directory. Here's why I dont know how to debug a user-control, since it has to be put first in the bin directory.

This is strange have you run it locally on your development PC and if so what the error you get off that? The source I uploaded compiled fine on my PC, may be another unrelated problem but running on you local PC should give a more detailed error

EDIT: Did you recompile the source I uploaded because I didn't attach any .DLL files within the .zip

Thanks

Andy
 
Yes, I recompiled the project.

I also use my pc as a server. I just couldn't send you to "http://localhost/...".

I dont understand why it would work on my computer. Here's my computer:

-winxp
-VS.net 2003
-.net framework 1.1

So I code on my computer, and I run the web site on my computer.

any idea where it comes from?
 
Can you enable remote debugging as follow may give me some more insite

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

Thanks

Andy
 
a_jam_sandwich said:
Now that is strange. Just got home from work and tried it and it works perfectly no problems at all. if you look in the /BIN is there a webapplication1.dll file?

Andy

Yes, there is a WebApplication1.dll in the BIN directory. Is my control supposed be in this DLL?

I really dont understand this.

Can you tell me if there's a problem with windwos XP, as a server?
Or maybe Visual studio 2003, or the 1.1 framwork.
 
To be honest it appears like its not seeing the DLL file and yes the control is within the WebApplication1.dll file it seems very strange I suggest it maybe a .NET security policy maybe try reinstalling the framework.

Ill try on my webserver today this is also a win2003 server box with .NET 1.1 and let you know the results.

EDIT: I have run the test on my dedicated webserver Test Server and this runs perfectly fine, it would seem like a setup or security policy error you have.

Andy
 
Last edited:
a_jam_sandwich said:
To be honest it appears like its not seeing the DLL file and yes the control is within the WebApplication1.dll file it seems very strange I suggest it maybe a .NET security policy maybe try reinstalling the framework.

Ill try on my webserver today this is also a win2003 server box with .NET 1.1 and let you know the results.

EDIT: I have run the test on my dedicated webserver Test Server and this runs perfectly fine, it would seem like a setup or security policy error you have.

Andy



Thank you very much. I'm gonna try to reinstall the framework.

One last question: do you think It could be caused by windows XP ??? Winxp is not the best system to run a web site :-\
 
1) I reinstalled the framework (1.1). Didn't work.

2) I tried looking over some security problem. I gave the c:/winnt/temp directory all the rights It might need. I add the aspnet user, and VS developer user. I gave those users full control. Didn't solve the problem.

3) I tried to give all rights to the C: drive. aspnet user, and VS developer user. Didn't helped.

4) I tried to run the project on my friend's computer. Same config. Same OS. Same Visual studio 2003, same framework. Guess what: It worked fine! :confused:

I still dont know what to do. I think you're right about the security thing. But what should I do??? :-\
 
Right you can reset the security policy via this

Open Control Panel -> Advanced Tools -> .NET Framework Configuration 1.1

Expand My Computer -> Right Click on Runtime Security Policy -> Reset All

Give that a try and restart your PC

Hope that helps

Andy
 
a_jam_sandwich said:
Right you can reset the security policy via this

Open Control Panel -> Advanced Tools -> .NET Framework Configuration 1.1

Expand My Computer -> Right Click on Runtime Security Policy -> Reset All

Give that a try and restart your PC

Hope that helps

Andy

Unfortunatly, It doesn't work :(

I'm still searching on Google. I leave the message error :

Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Parser Error 
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: File or assembly name WebApplication1, or one of its dependencies, was not found.

Source Error: 


Line 1:  <%@ Register TagPrefix="emailValidator" Assembly="WebApplication1"  Namespace="WebApplication1" %>
Line 2:  <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
Line 3:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 

Source File: c:\inetpub\wwwroot\vooral\WebApplication1\WebForm1.aspx    Line: 1 

Assembly Load Trace: The following information can be helpful to determine why the assembly 'WebApplication1' could not be loaded.


=== Pre-bind state information ===
LOG: DisplayName = WebApplication1
 (Partial)
LOG: Appbase = file:///c:/inetpub/wwwroot/vooral
LOG: Initial PrivatePath = bin
Calling assembly : (Unknown).
===

LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Post-policy reference: WebApplication1
LOG: Attempting download of new URL file:///C:/WINNT/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/root/1b7fde38/a3be0901/WebApplication1.DLL.
LOG: Attempting download of new URL file:///C:/WINNT/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/root/1b7fde38/a3be0901/WebApplication1/WebApplication1.DLL.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/vooral/bin/WebApplication1.DLL.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/vooral/bin/WebApplication1/WebApplication1.DLL.
LOG: Attempting download of new URL file:///C:/WINNT/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/root/1b7fde38/a3be0901/WebApplication1.EXE.
LOG: Attempting download of new URL file:///C:/WINNT/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files/root/1b7fde38/a3be0901/WebApplication1/WebApplication1.EXE.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/vooral/bin/WebApplication1.EXE.
LOG: Attempting download of new URL file:///c:/inetpub/wwwroot/vooral/bin/WebApplication1/WebApplication1.EXE.

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
 
Back
Top