0

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?

2 Answers 2

1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.