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?
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.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
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:
steddyman
Member
23 Points
82 Posts
Tracking DynamicControl change events
Nov 16, 2012 03:16 PM|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?
Thanks
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Tracking DynamicControl change events
Nov 16, 2012 03:23 PM|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.
Always seeking an elegant solution.
steddyman
Member
23 Points
82 Posts
Re: Tracking DynamicControl change events
Nov 16, 2012 03:28 PM|LINK
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.
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Tracking DynamicControl change events
Nov 16, 2012 03:37 PM|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.
Always seeking an elegant solution.
steddyman
Member
23 Points
82 Posts
Re: Tracking DynamicControl change events
Nov 16, 2012 04:18 PM|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); }sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Tracking DynamicControl change events
Nov 16, 2012 04:23 PM|LINK
Hi Steadyman, can you send me your page, my e-mail address is on my blog :)
Always seeking an elegant solution.
steddyman
Member
23 Points
82 Posts
Re: Tracking DynamicControl change events
Nov 16, 2012 04:28 PM|LINK
Just emailed you.
Many thanks for taking a look.
steddyman
Member
23 Points
82 Posts
Re: Tracking DynamicControl change events
Nov 16, 2012 05:09 PM|LINK
Fixed it!
I forgot I needed to set the AutoPostBack property of the DropDownList to True.
Thanks for your help once again.