Failure to create event handler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a very simple web page (ASP.NET) that I am trying to build. On the
web page is a checkbox that enables or disables other controls based upon the
checked status. However, .NET fails to create the event handler for the
CheckedChanged event.

In addition, even if I manually create the event handler, the CheckedChanged
event is never executed at runtime. AutoPostBack is set to True. Sometimes,
..NET "loses" blocks of code that I have painstakingly created.

And, just to add to my fun, the IDE consistently goes "whacky" on me -
reserved words lose their highlighting in the middle of editing and
IntelliSense refuses to display lists of the events and methods for objects.

I am using Visual Studio .NET 2003 Enterprise Architect with ASP.NET and C#.
Microsoft Development Environment 2003 Version 7.1.3088 and Microsoft .NET
Framework Version 1.1.4322 SP1

My PC (Dell OptiPlex GX280, 512MB RAM) is running Win2K Pro (Version 5,
Build 2195, SP4).

I must admit that I am VERY frustrated with this product - the IDE is buggy
as hell. If my company didn't insist upon using .NET, I would switch back to
VFP and be done with this nonsense once and for all.

Any help that is offered is *greatly* appreciated.

-RW-
 
Richard,

Per haps you can show us your code, and we could help you figure out why the
event handler was never executed. The one major reason why it might be the
case is, the event handler was not registered with the control. This happens
if you remove the control from the page, using the IDE. So, if your event
handler looks like this

private void MyHandler(object sender, EventArgs e)
{
}

check for a line something like:

myControl.Load+= new EventHandler(MyHandler);

In almost all cases I have seen the event handlers not firing, the absence
of the above line has been the cause. The cause for this cause, is when you
remove a control from the page (in IDE), the IDE also removes all the
registered event handlers for that control.

Hope that helps!
 
Manohar,

Thanks for the speedy reply! Here is the code I am using:

this.CheckBox1.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged)


private void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
Debug.WriteLine("Checkbox1 was changed...");
}

This code never fires.

-RW-
 
Richard,

do you have viewstate enabled/disabled? The event wiring in asp.net is
based on having viewstate enabled

-Stefan
 
As far as manually adding the handler - are you re-adding the handler on
every load of the page? You need to.

You might want to reinstall VS.NET. I don't think what you are experiencing
is typical.
 
The handler is being created in the InitializeComponent() section. Are you
saying that this needs to be moved to the Page_Load section?

I have re-installed VS.NET 2 times in the past week - that was a ton of fun,
can't wait to do it again <g>.

-RW-
 
Are you having this problem only
- for this specific checkbox control?
- for this specific page?
- or for all controls (buttons/links ...)?

Also, do you do anything else "fancy" which could manipulate your
viewstate?
E.g. are you dynamically injecting controls to your page (ie. a page
decorator)
using an http module or any other mechanism? If so, this could corrupt
your
viewstate (which would lead to your event not firing) ...

Also, is there any other code that clears the CheckedChange delegate
(search for "CheckBox1.CheckedChanged")?

The code you posted definitely looks ok
 
If you are manually adding the event handler, then why would it be in
InitializeComponent? That code gets regenerated by VS.NET, you shouldn't be
writing your own code and putting it there.
 
Could it be because the "Initialize" is never called? See what the page's
"AutoEventWireup" is set to, and switch it between true and false to see if
that makes a difference.
 
Marina,

VS.Net is generating event handlers within the InitializeComponent()
method.
I understand Richard just added it manually because VS.Net did not add
it
in VS.Net.

At runtime it doesn't matter who put the code in InitializeComponent -
be it Richard, be it VS.Net, since the code looks ok, the event should
fire.

Regards
-Stefan
 
Yes, it should fire. But if VS.NET has had a change to regenerate that code,
it may have deleted it. Which is the reason there is that warning there not
to change that method.

My point was just that manually adding the handler should work just fine.
And that maybe the reason it isn't working is because it is being deleted
when the designer code is regenerated.
 
Stefan is correct, I manually put the code there because that's where VS.NET
is putting all the other event handler declarations. Tracing the code at
runtime shows that the declaration is being executed. However, the method it
declares never fires when I click the checkbox. Hence, my problem...

-RW-
 
Back
Top