locked
Tracking DynamicControl change events RRS feed

  • Question

  • User-1530016532 posted

    I have a form with a mix of DynamicControls bound to LINQ for SQL and standard controls.  I need to trigger an event handler when the DropDown control created by the DynamicControl changes value, so I can update a Label field with a calculated value.

    I can't see any of the standard events for these controls so how could I hook into the control value changing?

    Thanks

    Friday, November 16, 2012 11:16 AM

Answers

  • User-330204900 posted

    I woudl do that in either Page_Init or Page_Load as any later than that and I don;t seem to recive the events most of the time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 11:37 AM
  • User-1530016532 posted

    It doesn't seem to be working for me for some reason.  I can't seem to register the handlers in Page_Init even after the RegisterControl command is issued, because it reports that the DropDownList1 control is not found.

    I can register them in Page_Load, but when I place breakpoints in the handlers, they are never being called when I change the drop downs.  This is the code I am using:

        protected void AddChangeHandlers()
        {
            DynamicControl dcCPU = ((DynamicControl)FormView1.FindControl("dcCPU"));
            DropDownList dlCPU = ((DropDownList)dcCPU.FieldTemplate.FindControl("DropDownList1"));
            DynamicControl dcMemory = ((DynamicControl)FormView1.FindControl("dcMemory"));
            DropDownList dlMemory = ((DropDownList)dcMemory.FieldTemplate.FindControl("DropDownList1"));
            dlCPU.SelectedIndexChanged +=new EventHandler(CPUMemDropdown_SelectedIndexChanged);
            dlMemory.SelectedIndexChanged += new EventHandler(CPUMemDropdown_SelectedIndexChanged);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 12:18 PM
  • User-1530016532 posted

    Fixed it!

    I forgot I needed to set the AutoPostBack property of the DropDownList to True.

    Thanks for your help once again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 1:09 PM

All replies

  • User-330204900 posted

    Hi steddyman, there are no standard events for this but if you fine the DynamicControl and then ectract the field template you can then cast the DataControl to DropDownList and then add an event handler to that's selection changed event.

    Friday, November 16, 2012 11:23 AM
  • User-1530016532 posted

    Hi

    Thanks for the quick response.  That makes sense.

    What would be the best place for me to insert that event handler into within the Standard page flow?  I've only ever wired up events declaratively, not from code for controls.

    Friday, November 16, 2012 11:28 AM
  • User-330204900 posted

    I woudl do that in either Page_Init or Page_Load as any later than that and I don;t seem to recive the events most of the time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 11:37 AM
  • User-1530016532 posted

    It doesn't seem to be working for me for some reason.  I can't seem to register the handlers in Page_Init even after the RegisterControl command is issued, because it reports that the DropDownList1 control is not found.

    I can register them in Page_Load, but when I place breakpoints in the handlers, they are never being called when I change the drop downs.  This is the code I am using:

        protected void AddChangeHandlers()
        {
            DynamicControl dcCPU = ((DynamicControl)FormView1.FindControl("dcCPU"));
            DropDownList dlCPU = ((DropDownList)dcCPU.FieldTemplate.FindControl("DropDownList1"));
            DynamicControl dcMemory = ((DynamicControl)FormView1.FindControl("dcMemory"));
            DropDownList dlMemory = ((DropDownList)dcMemory.FieldTemplate.FindControl("DropDownList1"));
            dlCPU.SelectedIndexChanged +=new EventHandler(CPUMemDropdown_SelectedIndexChanged);
            dlMemory.SelectedIndexChanged += new EventHandler(CPUMemDropdown_SelectedIndexChanged);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 12:18 PM
  • User-330204900 posted

    Hi Steadyman, can you send me your page, my e-mail address is on my blog :)

    Friday, November 16, 2012 12:23 PM
  • User-1530016532 posted

    Just emailed you.

    Many thanks for taking a look.

    Friday, November 16, 2012 12:28 PM
  • User-1530016532 posted

    Fixed it!

    I forgot I needed to set the AutoPostBack property of the DropDownList to True.

    Thanks for your help once again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2012 1:09 PM