Last post Nov 16, 2012 01:09 PM by steddyman
Nov 16, 2012 11:16 AM|steddyman|LINK
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?
Nov 16, 2012 11:23 AM|sjnaughton|LINK
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.
Nov 16, 2012 11:28 AM|steddyman|LINK
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.
Nov 16, 2012 11:37 AM|sjnaughton|LINK
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.
Nov 16, 2012 12:18 PM|steddyman|LINK
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);
Nov 16, 2012 12:23 PM|sjnaughton|LINK
Hi Steadyman, can you send me your page, my e-mail address is on my blog :)
Nov 16, 2012 12:28 PM|steddyman|LINK
Just emailed you.
Many thanks for taking a look.
Nov 16, 2012 01:09 PM|steddyman|LINK
I forgot I needed to set the AutoPostBack property of the DropDownList to True.
Thanks for your help once again.