Cross user control communication via parent control

Z

zeya_bakhtyar

Here is the page architecture:
Page loads multiple user controls (including nested user controls)
dynamically based on parameters provided into place holders. Note: The
page only has the logic to load the user controls and subscribe to
custom event exposed in the user controls.

Scenario:
User comes to this page fills the fields generated by multiple user
controls and then hits "Save" button in one of the forms.

Question:
- How can all the information be collected in the control that has the
save button and saved to a repository?
- Is there a better practise?
- How can all the user controls be "wired" together to cross
communicate?

An example will be great help.

- Working with ASP.Net 2.0 using C#

Thanks in advance.
 
J

John Saunders [MVP]

Here is the page architecture:
Page loads multiple user controls (including nested user controls)
dynamically based on parameters provided into place holders. Note: The
page only has the logic to load the user controls and subscribe to
custom event exposed in the user controls.

Did you not see my reply to you? Here it is again:
-------------
zb said:
Here is the page architecture:
Page loads multiple user controls (including nested user controls)
dynamically based on parameters provided into place holders. Note: The
page only has the logic to load the user controls and subscribe to
custom event exposed in the user controls.

Scenario:
User comes to this page fills the fields generated by multiple user
controls and then hits "Save" button in one of the forms.

Question:
- How can all the information be collected in the control that has the
save button and saved to a repository?
- Is there a better practise?
- How can all the user controls be "wired" together to cross
communicate?

In the same way that you exposed the events from the user controls, you can
expose properties. Add properties to the user controls which will return the
data they have gathered from their input controls. The controlling page can
read those properties and use them to write to the repository.

I suggest that you do not do what is done by some code I know (you know who
you are!). Do not have a Save method on each user control, that accepts a
DataTable. The user control can fill the DataTable with the information it
has gathered.

The problem is that you have now closely tied each individual user control
to the structure of the data in your repository. It is better to separate
data from presentation. Instead, either have separate properties, or have a
property that returns a Data Transfer Object (DTO) from the user control. It
should return a struct containing all of the data collected by the user
control. Your calling page can take that data and put it into your
repository however it likes. The nice thing is that the user controls will
not need to change if the details of your data access layer change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top