locked
Events triggered incorrectly RRS feed

  • Question

  • User-398950277 posted

    I have a custom control containing some checkboxes. To each checkbox I have added an eventhandler for CheckChanged. The controls are added to an updatepanel. Everything is created dynamically.

    My problem is that the events are not generated as they should. Let's say I check three of the boxes. After the first check I get one checked event. But after the second check I get one checked event for the first box again and then one for the second box. After checking the third box I get all three checked events, not only the last one, which I should. If unchecking a box, I dont get any unchecked event for that box, but instead checked events for all the other boxes that are checked.

    I have enabled viewstate for the controls, but it seems as the viewstate variables are lost anyway.

     Any ideas??

    Thursday, March 27, 2008 5:21 AM

All replies

  • User541108374 posted

    Hi,

    does your control work without using it inside an UpdatePanel?

    Grz, Kris. 

    Thursday, March 27, 2008 6:08 AM
  • User-398950277 posted

    No, same thing happens without updatepanel.

    When I try it in my testapp it works fine, the events are triggered as they should and the viewstate is maintained. But when I try to plug it into my system it fails.

    Thursday, March 27, 2008 6:21 AM
  • User-398950277 posted

    A clarfification: the viewstate of the checkboxes are maintained as they should. It is a separate viewstate variable that is lost (but not in the test app)

    Thursday, March 27, 2008 8:30 AM
  • User-1684099974 posted

    Can you post your source code?

    Thursday, March 27, 2008 9:20 AM
  • User-398950277 posted

    My control derives from CompositeControl and overrides CreateChildControls. This is where the checkboxes and eventhandlers are created and added. This is called everytime the user click on a box.

    protected override void CreateChildControls()
            {
                controls = new Dictionary<GUID, WebControl>();
    
                foreach (ClassificationItemRecord record in getFlatClassificationStructure(DataSource))
                {
                    if (record.IsSelectable)
                    {                   
                        CheckBox cb = new CheckBox();
                        cb.Text = record.DisplayName;
                        cb.ID = record.IID.ToString();
                        cb.AutoPostBack = true;
                        
                        cb.EnableViewState = true;
                        cb.Checked = isChecked(record.IID);
                        
                        cb.CheckedChanged += cb_CheckedChanged;
                        RenderBase.AddControl(Controls, cb);
    
                        controls.Add(record.IID, cb);
                    }
                    else
                    {
                        Label lb = new Label();
                        lb.ID = record.IID.ToString();
                        lb.Text = record.DisplayName;
    
                        controls.Add(record.IID, lb);
                    }
                }
            }

    My eventhandler cb_CheckedChanged handles the events fired when a checkbox is clicked. However, this event is triggered for ALL checked boxes everytime and never for the "unchecked event". So, if three boxes are checked and you uncheck the third, events for the two first are triggered.

    void cb_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox cb = (CheckBox)sender;
                if (cb.Checked)
                {
                    AddSelectedRecord(cb.ID);
                }
                else
                {
                    RemoveSelectedRecord(cb.ID);
                }
    
                creator.UpdateInterOperativeFields();
            }
     

    AddSelectedData and RemoveSelectedData basically adds and removes the selected items stored in a list in a viewstate variable. This is however cleared when getting to the server side next time:

    private List SelectedDataList
            {
                get {   return ViewState["SelectedDataList"] as List; }
    
                set { ViewState["SelectedDataList"] = value; }
            }  
     

    The DataSource is cleared as well.

    public ClassificationItemRecord[] DataSource
            {
                get { return (ClassificationItemRecord[])ViewState["DataSource"]; }
                set { ViewState["DataSource"] = value; }
            }
     
    Thursday, March 27, 2008 9:58 AM
  • User-398950277 posted

    No ideas anyone?

    All boxes that have been checked trigger their events with every postback. No event is triggered for unchecked boxes.

    Friday, March 28, 2008 9:25 AM
  • User-398950277 posted

    I have tried doing some more research of what happens in the application. To simplify the scenario I only use two checkboxes when testing.

    The application has a custom control (called A). In A's OnInit method I create the two checkboxes and attach eventhandlers - everything works fine, the events are triggered as supposed to.

    Control A's OnInit method also creates several other custom controls (called B). When I create two checkboxes inside B's OnInit method (where they actually should be created) the events are triggered incorrectly. If a checkbox is checked its event is triggered with every postback, and unchecking a box never triggers an event... :(

    Ideas??

    Monday, March 31, 2008 6:15 AM