I have two user controls on the same page. One contains a ListView that displays navigation links, the second user control should be updated when user clicks on the buttonlink in the ListView. How can I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could pass necessary informations from UserControl A to page via EventArgs(or pass the UserControl itself as argument and use it’s public properties).
The page then passes the arguments to UserControl B via method parameter or by changing it’s public properties before calling the
Update-Method.Here is the sample code you’ve requested.
Sorry for the meaningless naming but you haven’t told what’s this all about. You should use readable variables,properties,method and event-names instead.
Reduced UserControl A with a ListView:
Removed the ListView databinding from codebehind because that doesn’t matter. The important part is handling the ListView’s ItemCommand and raising the custom event:
Simple UserControl B with nothing more than a Label(ascx):
With an
Update-Method in codebehind:Finally, here is the Page(aspx)
It controls both UserControls. It handles the event from UserControl A and calls the
Update-Method that UserControl B provides:The advantage of this event-approach is that UserControls stay being reusable. You can use UserControl A in other pages as well even when they don’t handle this event. It’s part of the controller to decide what is needed and what should be done.
UserControls as a rule should not depend on specific controllers, otherwise they are hard-linked and not reusable. That would be also a good source for nasty erros. A UserControl might be a controller for other nested (User-)Controls but not for the page itself.
Communication Summary: