views:

240

answers:

2

I have a user control that displays results from a database in a gridview. The containing page receives querystring values and passes them to the user control to load.

From the containing page, what is the best way to load and display the user control? Currently, I do the following:

  1. Containing Page_Load: Get querystring values
  2. Containing Page_Load: Instantiate User Control
  3. Containing Page_Load: set User Control's properties
  4. Containing Page_Load: Add User Control to Page

I'm not sure what event in the user control to use to hit the database and populate control; should I make a "Populate this Control" Method on the usercontrol and call it from the Containing page? Or use a Page_Load routine in the user control?

+1  A: 

If your page is not doing anything to sanitize or process the variables, is there any reason that the user control cannot just grab the values itself?

If you do have a reason to sanitize the data, you could load in the controls' Load event, but in this case since the page appears to be driving the functionality of the user control you might benefit from a "SetupControl" or some other method that you can call after it has been configured.

Mitchel Sellers
A: 

Override the OnDataBinding method in the user control to access the database and populate the control from the results. You could alternatively have the control handle the DataBinding event.

In other words, treat the user control just like any other control.

John Saunders