wrote:
> I have two UserControls sitting alongside one another on a ContentPage,
> loaded within a MasterPage
>
> My two UserControls (and the dozen other controls on my site) share
> some common code so I have created a Class called MyUserControl that
> Inherits System.Web.UI.UserControl, and my UserControls inturn inherit
> MyUserControl.
>
> I am making extensive use of Namespaces so the Class ContentPage is
> named Project.Pages.MyPage and the two UserControl are named
> Projects.Controls.Group.MyControl1 and
> Projects.Controls.Group.MyControl2
>
> MyControl1 contains a Method 'DoStuff' that populates a GridView from
> the database and MyControl2 contains a form to insert a new record. On
> update I want to be able to call the DoStuff method to repopulate the
> GridView.
>
> My problem is that I have been unable to find a technique that allows
> me to call the MyControl1.DoStuff method from MyControl2.
>
> I have come close by adding a Delegate and Event to the MyControl2
> Class:
>
> Public Shared Event Updated As UpdateEventHandler
> Delegate Sub UpdateEventHandler()
>
> I raise the event at the of the database update with:
>
> RaiseEvent Updated()
>
> I have been unable to add an event handler to MyControl1, but I was
> able to add an event handler to the ContentPage MyPage by adding the
> following to the OnLoad method:
>
> AddHandler Projects.Controls.Group.MyControl2.Updated, AddressOf
> UpdateSummary
>
> And then adding the following Method to the MyPage Class:
>
> Private Sub UpdateSummary()
> Dim Ctr As New Projects.Controls.Group.MyControl1
> Ctr =
> CType(Page.Controls(0).FindControl("myPage").FindC ontrol("MyControl1"),
> Projects.Controls.Group.MyControl1)
> Ctr.DoStuff()
> End Sub
>
> The Event is triggered correctly, finds the control in the page
> hierarchy, casts to MyControl1 and calls the method DoStuff, but when
> called all of MyControl1 controls are Null and unable to be accessed.
>
> I must be missing something, because MyControl1 has already been loaded
> in the Control lifecycle and I should be able to find this, without
> using the 'As New' directive.
>
> I have also been experimenting with adding a <%@ Reference
> Control="~/controls/group.MyControl1.ascx" %> to MyControl2 and
> attempting to accessing the MyControl1.DoStuff method directly from
> MyControl2, but havn't been able to get that to work either.
>
> What is the best technique to call a Method in a loaded UserControl
> from another UserControl?
>
> Thanks,
>
> Stephen
Solved my own problem.
When declaring the UserControl in the ContentPage, you must use an ID
and not rely on the Class name when using FindControl. ie:
<uc1:usercontrol1 id="MyControl1" runat="server" />