Do not fill DropDownList if upstanding UserControl isn't visible
-
16-09-2019 - |
Question
I have page Add.aspx containing a number of UserControls
: AddRequest.ascx, AddOperation.ascx, AddObject.ascx, etc. Depending on Request["type"]
one control becomes visible.
Each UserControl
contains a number of DropDownList
s that are being filled via SqlDataSource
from DB. For example, types, statuses, currencies, etc.
It seems that appropriate SqlDataSource
s queries DB even if it's UserControl
-owner isn't visible. So having n
controls, only 1
query is really needed and n-1
are not.
How can I change this behavior?
Solution
If I understood correctly, you should only load the user control you are going to display. Something like:
Control myControl = LoadControl("SomeControl.ascx");
You can then add the control to a placeholder. This way you avoid loading unnecessary controls to your page and their round trips to the database are also not executed.
OTHER TIPS
Try making the loading of the usercontrols imperative in nature (as demonstrated by @Dante) instead of declarative (by registering it in the ASPX, for instance). That way, only that control which is explicitly loaded should invoke it's respective Datasource controls).