Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to call a Method in a UserControl from another UserControl

Reply
Thread Tools

How to call a Method in a UserControl from another UserControl

 
 
jsausten@hotmail.com
Guest
Posts: n/a
 
      08-09-2006
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

 
Reply With Quote
 
 
 
 
jsausten@hotmail.com
Guest
Posts: n/a
 
      08-11-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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" />

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to call a method within another method Paul Roche Ruby 3 10-06-2010 04:02 AM
accessing usercontrol from another usercontrol Phl ASP .Net 2 11-18-2004 07:33 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
Help with a utility class and having a method call another method from within Ian Python 2 09-06-2003 03:35 PM



Advertisments