How to call a Method in a UserControl from another UserControl

Discussion in 'ASP .Net Web Controls' started by jsausten@hotmail.com, Aug 9, 2006.

  1. Guest

    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").FindControl("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
     
    , Aug 9, 2006
    #1
    1. Advertising

  2. Guest

    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").FindControl("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" />
     
    , Aug 11, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nail
    Replies:
    0
    Views:
    1,584
  2. Phl
    Replies:
    2
    Views:
    549
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  3. Fabiano
    Replies:
    2
    Views:
    652
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  4. Ian
    Replies:
    2
    Views:
    366
    Ulrich Petri
    Sep 6, 2003
  5. Paul Roche
    Replies:
    3
    Views:
    148
    Jeremy Bopp
    Oct 6, 2010
Loading...

Share This Page