944,089 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1648
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2009
0

Evenhandler Problem

Expand Post »
I am creating a gridview dynamically using Itemplate but I have run into an issue where the event handlers I create for my drop down boxes in edititem template are accumulating every time it fires.

To be more specific. When I enter edit mode and select a new item in the dropdownlist the event handler fires twice (this I know is because the event handler is updating a second dropdownlist), but if I change another dropdownlist during the same edit session or change the same dropdownlist again the new event fires then the original two fire again. Then if I make a third change I get four events and it keeps incrementing.

I am removing the orignal event handler before adding a new event handler so there shouldn;t be multiple handlers for each instance.

C# Syntax (Toggle Plain Text)
  1. else if (ControlType == "DropDown")
  2. {
  3. var dropVals = new Dictionary<string, string>();
  4. DropDownList field_dropbox = new DropDownList();
  5.  
  6. field_dropbox.ID = FieldName;
  7. field_dropbox.SelectedIndexChanged -= new EventHandler(field_dropbox_SelectedIndexChanged);
  8. field_dropbox.SelectedIndexChanged += new EventHandler(field_dropbox_SelectedIndexChanged);
  9. field_dropbox.AutoPostBack = true;

My event handler updates another dropdown so I know why the event should fire twice but it doesn't explain why they keep firing.

C# Syntax (Toggle Plain Text)
  1. protected void field_dropbox_SelectedIndexChanged(Object sender, EventArgs e)
  2. {
  3. string syncDrop = "";
  4. string list = "";
  5. var _sync = new Dictionary<string,string>();
  6.  
  7. _sync = GlobalVars._DropSync;
  8. list = ((DropDownList)sender).ID.ToString();
  9.  
  10. if (_sync.ContainsKey(list))
  11. {
  12. syncDrop = _sync[list].ToString();
  13.  
  14. GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
  15.  
  16. // Get the reference of the first DropDownlist that will generate this SelectedIndexChanged event
  17. DropDownList dropdownlist1 = (DropDownList)gvr.FindControl(list);
  18.  
  19. // Get the reference of second DropDownlist in the same row.
  20. DropDownList ddlSync = (DropDownList)gvr.FindControl(syncDrop);
  21.  
  22. if (ddlSync != null)
  23. {
  24. ddlSync.ClearSelection();
  25. //ddlParagraph.Items.FindByText(dropdownlist1.SelectedItem.Value.ToString()).Selected = true;
  26. ddlSync.Items.FindByText(dropdownlist1.SelectedValue.ToString()).Selected = true;
  27. }
  28.  
  29. }
  30. //new Page().Session["EventhandlerFired"] = ((DropDownList)sender).ID;
  31.  
  32.  
  33.  
  34. }

Is there a way to purge the eventhandler after it is fired so it does not fire if I make a subsequent change?
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Oct 31st, 2009
0
Re: Evenhandler Problem
Click to Expand / Collapse  Quote originally posted by cgyrob ...
I am removing the orignal event handler before adding a new event handler so there shouldn;t be multiple handlers for each instance.

C# Syntax (Toggle Plain Text)
  1. else if (ControlType == "DropDown")
  2. {
  3. ...
  4. field_dropbox.SelectedIndexChanged -= new EventHandler(field_dropbox_SelectedIndexChanged);
  5. field_dropbox.SelectedIndexChanged += new EventHandler(field_dropbox_SelectedIndexChanged);
  6. ...

In the above, I don't understand why you are removing the event handler and then adding the same event handler right back...


If you want to stop the handler from processing when you are programmatically changing the item that causes the event to fire again, you can set a variable that indicates you are doing just that:

C# Syntax (Toggle Plain Text)
  1. bool bInSelectedChangedEvent = false;
  2.  
  3. protected void field_dropbox_SelectedIndexChanged(Object sender, EventArgs e)
  4. {
  5. if (!bInSelectedChangedEvent)
  6. {
  7. // set flag to true to prevent processing while we are changing the item that causes the event to fire again...
  8. bInSelectedChangedEvent = true;
  9.  
  10. // all the processing code in here...
  11. }
  12. bInSelectedChangedEvent = false;
  13. }

However, you might have a design problem if you are modifying another control that is tied to the same event handler, which could then modify another control that is tied to the same event handler, which could then modify another control that is tied to the same event handler, which could then....etc. etc. etc.


Click to Expand / Collapse  Quote originally posted by cgyrob ...
Is there a way to purge the eventhandler after it is fired so it does not fire if I make a subsequent change?

Not sure what you mean by purge, but if you can't use the recommendation above and you want to remove all event handlers associated with the control's event, just assign it null:

C# Syntax (Toggle Plain Text)
  1. comboBox1.SelectedIndexChanged = null;

The above null assignment will remove all event handlers from that event member.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 31st, 2009
0
Re: Evenhandler Problem
DdoubleD -- Are you sure about removing the event handler?
C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. comboBox1.SelectedIndexChanged = null;
  4. }

That code won't compile for me. In my Delphi days that is exactly how you cleared event handlers and I like that design but I have never figured out a way you could do it in .NET. As far as I know you have to reflect in to the event handler so you can -= all event handler references. Could you please elaborate on it? I would really like to know if something like that could be done

[edit]
cgyrob -- Can you upload a sample application demonstrating the problem?
[/edit]
Last edited by sknake; Oct 31st, 2009 at 10:29 am.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 31st, 2009
0
Re: Evenhandler Problem
Click to Expand / Collapse  Quote originally posted by sknake ...
DdoubleD -- Are you sure about removing the event handler?
C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. comboBox1.SelectedIndexChanged = null;
  4. }

That code won't compile for me. In my Delphi days that is exactly how you cleared event handlers and I like that design but I have never figured out a way you could do it in .NET.
You are right, that won't work! I had somewhere you could this, but had never tried to do it--obviously. I agree it would be nice. Good catch sknake!
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Oct 31st, 2009
0
Re: Evenhandler Problem
Dang

I was hoping there was something I hadn't tried with clearing event handlers. They are one of the biggest culprits in object references being maintained and stopping garbage collection, ie memory leaks.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 31st, 2009
0
Re: Evenhandler Problem
@DdoubleD

Thanks for the reply. i added the removal on the eventhandler on Friday because i found a blog with someone who had a similar problem and adding the removal of the eventhandler helped him. It had no affect on my code at all which is why I finally decided to ask around.

As for the purging I just want to remove any reference to the event once it is processed so it does not run again if I make subsequent changes to any drop downs within the same sequence. I should be able to change a dropdown over and over with only firing the current event only.

@Sknake
I will look at modifying my code on Monday to run without my AD and DB connections.

Thanks for the replies.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 2nd, 2009
0
Re: Evenhandler Problem
I tried one more thing before sending out this copy. I tried to create different event handlers if the field was an ID field or a DESC field but the page still has the same functionality which is really making me scratch my head.

Now Instead of firing the same event handler twice with the ID field firing first it is firing the ID event handler first even if I change the DESC dropdown. I can change the ID field anytime but can only change the DESC if I try it first.

I was able to pull this page out of my project and modify it enough to work using some lists and one table. I provided a script to create and load one table. I also changed the program to use Sqlserver as I know there are more people with access to sql server then oracle.

If anyone has any ideas it would be appreciated because I am totally confused why it is functioning like this.
Attached Files
File Type: zip Eventhandlerissue.zip (372.0 KB, 28 views)
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 3rd, 2009
0
Re: Evenhandler Problem
The only thing I could think of is that your old values may be stored in the viewstate and when you fire it is not accumulating events for every change you make but rather for each dropdown you change.

Example:
DrownA Original Value: "A"
DrownB Original Value: "A"
DrownC Original Value: "A"

New Values:
DrownA Original Value: "Z"
DrownB Original Value: "Z"
DrownC Original Value: "Z"

The event will fire three times:
DrownA Original Value: A -> Z
DrownB Original Value: A -> Z
DrownC Original Value: A -> Z

This code also raises an exception sometimes:
C# Syntax (Toggle Plain Text)
  1. if (ddlSync != null)
  2. {
  3. ddlSync.ClearSelection();
  4. ddlSync.Items.FindByText(dropdownlist1.SelectedValue.ToString()).Selected = true;
  5. }

As for how to fix this I don't know -- I don't understand what the problem is exactly. I am able to modify fields and it retains the changes, and the item count in the dropdown isn't accumulating. Can you step me through reproducing this behavior?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 3rd, 2009
0
Re: Evenhandler Problem
The exception is caused because the test data in the table does not match all the dropdownlist, I don't have any issues with the full dataset.

To re-create

The problem is only with the drop down lists I am trying to keep in sync, all the other lists work fine.

When you open the app in edit mode, first change the value in the drop down list cpm_line_name, this will work correctly the first time. Now try changing this value again, it will not work for any subsequent updates as the event handler for cpm_line_id fires first changing the value back to the original before firing its own event. I can change the line_id value updating the name field correctly as many times as I want. The problem only occurs on the name field that I am trying to keep in sync.

If you step through you will see that the event handler for the line id fires first every time.

I was perplexed as to why the line_id event fired first every time so I changed my select to pull the line_name first in the row and then the line_name event fired first every time and I could not update the line_id only the line_name.

For some reason the firing of the events seem to be linked to the order of the fields in the row created in the template and not the sequence in which thy were triggered.

Never heard any mention of this while trying to figure out this problem.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 3rd, 2009
0
Re: Evenhandler Problem
@sknake - a little update.

After looking further into my problem I believe this has to do with Itemplate is controlling the events for all the controls that are held in it; this is why the events fire in the same order everytime.

I read that I need to bubble up the event to the page so they will be handled by the page and not by the template control. I have never used this technique but I will give it try and let you know how it worked.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to create mysql db programmably
Next Thread in C# Forum Timeline: Security of Videos?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC