Link to home
Start Free TrialLog in
Avatar of bigd2001grad
bigd2001gradFlag for United States of America

asked on

Controls on my .ascx control become unbound from events

I can't seem to consistently reproduce this error, but whenever I edit the UI of an .ascx control and toggle to the code behind to make changes - some of my controls become "unbound" from their click events.  So, whenever I debug the web application, the controls (buttons) don't do anything when clicked.  This happens infrequently, but it is a pain to have to go back, create new click events and copy the code over from the inactive events to the new ones.

Some background:

Visual Studio 2003 Enterprise Architect
Language: ASP.NET 1.1, C# codebehind
Database: SQL Server 2000
Proficiency: Moderate - Advanced

This has happened to several members of my programming team and I've come across the issue before with other clients.

I appreciate the help and look forward to your feedback.
Avatar of bigd2001grad
bigd2001grad
Flag of United States of America image

ASKER

p.s.  For those of you familiar with ReSharper - I can usually tell something has happened when my warning light goes orange and several of my click event methods become highlighted.

- just thought I'd throw that in there...
Its getting reloaded each time you leave,, just like a web page...
are all the objects being disposed and re initiated properly?

try a custom click event? in the codebehind

They are all be handled properly.

Actually, it's the custom click events we usually have the problem with.  It is for this reason we don't put custom click events in InitializeComponent(). Below is a clipping of where we were having some of our problems.

override protected void OnInit(EventArgs e)
            {
                  InitializeComponent();
                                                ///we'd usually call a method that contained all of our custom click events here
                  base.OnInit(e);
            }
            
            private void InitializeComponent()
            {
                  this.ddlEdc.SelectedIndexChanged += new System.EventHandler(this.ddlEdc_SelectedIndexChanged);
                  this.btnDeleteIdrUsageDetail.Click += new EventHandler(btnDeleteIdrUsageDetail_Click);
                  this.btnViewUsage.Click += new System.EventHandler(this.btnViewUsage_Click);
                  this.btnViewNormalizedUsage.Click += new System.EventHandler(this.btnViewNormalizedUsage_Click);
                  this.btnSavePremise.Click += new System.EventHandler(this.btnSavePremise_Click);
                  this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
                  this.grdMonthlyUsageDetail.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdMonthlyUsageDetail_UpdateCommand);
                  this.grdMonthlyUsageDetail.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdMonthlyUsageDetail_CancelCommand);
                  this.grdMonthlyUsageDetail.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdMonthlyUsageDetail_EditCommand);
                  this.grdMonthlyUsageDetail.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdMonthlyUsageDetail_DeleteCommand);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
ASKER CERTIFIED SOLUTION
Avatar of 5thcav
5thcav
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One way would be to test in the page load if an object exists then fine,, if nothing then it surely passed OnInit. Maybe an error is not being caught,,, setup a try block
I attempted the catch block and no errors were thrown.  As for the init - I created a new one, tried some custom events and everything worked fine.  I haven't been able to consistently get my error to re-occur.  

Could this be more of a visual studio bug than a programming flaw?
It could be,

But the fact remains, if your InitializeComponent is not being called or some how not initializing the Event Handlers do to some kind of error it can be attributed to only a few things.

i can think of;

Your losing state in iis? bin being unloaded?
Error not being caught.

the parent form is not initializing the control? < Maybe a good place to look!
what if you created the control in code “dim something as your control” or be sure the control has been instantiated?.
After a little research...

InitializeComponent is being called, it is only the event handlers that are being "unwired".  In some cases it is because a user double clicks on a control and instead of being taken to the existing click event method, a new one is created and the pre-existing method is ignored in the background.  What confuses me is when event handlers and controls aren't touched, a new event handler is not written but the event handlers still become unwired.  Because ReSharper identifies the disconnected nature of the event handlers before I even compile, I can rule out IIS having anything to do with it.

Does that help clarify a little?
I contacted Microsoft about the issue and apparently there is a bug in VS 2003 that disconnects a function/event from its object while doing heavy lifting in the background.  The issue was resolved in VS 2005.  

Thank you 5thcav for pointing me in the right direction.