Last post Aug 22, 2007 01:31 PM by jrummell
May 18, 2006 09:42 AM|MartinSmithh|LINK
I have a very simple User Control with 2 buttons yet when I put it on a page in the designer it shows an error in place of the
(but renders the rest of the control OK)
The error message says "Error Rendering Control" An unhandled exception has occurred. Object reference not set to an instance of an object.
At runtime the control displays fine.
Is this a bug in ASP.NET or am I doing something wrong?
Steps to reproduce
Code behind (designer.cs)
Code Behind (other cs)
document.location.href = '"
May 18, 2006 11:35 AM|MartinSmithh|LINK
Often the first time I open the page containing the control it looks OK in design view.
Right clicking the page in design view and choosing "refresh" causes the problem.
I do have another (more complex) control which has this issue all the time however so I'm hoping that the solution to the simple control will shed light on the other case!
May 18, 2006 01:43 PM|PeterBrunone|LINK
May 18, 2006 02:23 PM|MartinSmithh|LINK
Actually I think it's a bug with asp:button as I have now commented out all code and I still get the issue!
Do you know what code actually runs then when a UserControl is shown in design view? e.g. if I happened to have code that updated a DB in Page_Load would that get called everytime someone opened a web form with that UC on it in VS?
I saw one of your other posts about debugging UserControls with 2 instances of VS but I couldn't get that to work either. Does that definitely work for UCs or just ServerControls?
May 18, 2006 03:45 PM|NC01|LINK
I can tell you up front that there is no bug with the asp:Button. You've got a problem somewhere in your code.
Do you have a public delegate declared for the event?
public delegate void EventHandler(object sender, System.EventArgs e);
BTW, I just tried the significant parts of your code with the delegate declared, and got no errors.
May 18, 2006 04:19 PM|MartinSmithh|LINK
I think you're right to point the finger at my event code as I am only getting this error on UserControls that raise events but adding the line as above doesn't resolve the issue.
(Did you try refreshing the page in design view)
May 19, 2006 08:58 AM|NC01|LINK
You have to have a delegate to add a custom event. Here are the steps for adding a custom event to a User Control:
// In the Control
// Add a public delegate for the event
public delegate void CustomEventHandler(object sender, System.EventArgs e);
// Add the event
public event CustomEventHandler CustomEvent;
// Add the event handler
// To invoke: this.OnCustomEvent(System.EventArgs.Empty);
protected void OnCustomEvent(System.EventArgs e)
if ( this.CustomEvent != null )
// In the parent container
// Add a reference to the control if a User Control:
// NOTE: VS doesn't do this for you for UCs
protected AUserControl SomeControl;
// Subscribe to the event
// NOTE: Do this in Page_Load and NOT in InitializeComponent
// as it can get erased on any change to the Designer.
SomeControl.CustomEvent += new control-classname.CustomEventHandler(SomeControl_CustomEvent);
// Add an event handler
private void SomeControl_CustomEvent(object sender, EventArgs e)
Aug 22, 2007 01:31 PM|jrummell|LINK
I had a similar issue with a control I created, a ConfirmButton class that extends Button and contains a ConfirmButtonExtender (from AJAX Toolkit). I was able to make the designer happy by add the Designer attribute:
public class ConfirmButton : Button