views:

73

answers:

2

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 DropDownLists that are being filled via SqlDataSource from DB. For example, types, statuses, currencies, etc.

It seems that appropriate SqlDataSources 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?

+1  A: 

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.

Dante
+2  A: 

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).

Cerebrus