Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   How to call a Method in a UserControl from another UserControl (http://www.velocityreviews.com/forums/t778675-how-to-call-a-method-in-a-usercontrol-from-another-usercontrol.html)

jsausten@hotmail.com 08-09-2006 06:56 PM

How to call a Method in a UserControl from another UserControl
 
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


jsausten@hotmail.com 08-11-2006 09:53 AM

Re: How to call a Method in a UserControl from another UserControl
 

jsausten@hotmail.com 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" />



All times are GMT. The time now is 08:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.