reference one custom user control from another

D

densial

I have two custom user controls, Control1.ascx and Control2.ascx.
Control1 has a public method RunMe(). When I add both controls to an
aspx I want to access the RunMe() method of Control1 from within a
Control2 method. Anyone have any ideas?
 
D

densial

http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2008/0...
Thanks for that, appreciate it, but further digging showed my problem
was self inflicted. I am using a master page so I had a generic static
method for doing a FindControl based on the Content section I was
within, AND to make things worse I had forgotten that I had 3 custom
user controls, Control1 was actually inside Control3, so every time I
tried to reference the Parent using my generic method it errored out.

anyways my code ended up as follow...

this is inside my .ASCX

<%@ Reference Control="Control2.ascx" %>

Then my method is this

protected void MyButton_Click(object sender, ImageClickEventArgs
e)
{
Control Control2Control = this.Parent.FindControl
("ControlID");
if (Control2Control != null)
{
Control2 FoundControl = (Control2)Control2Control;
Control2.RunMe();
}
}
 
G

Gregory Beamer

As long as you have the controls stacked, it is quite easy to run routines
from one control to the next. If you are going back through the parent, you
should rethink your architecture, as you are creating a nightmare.

In general, I put the working code into libraries. In some cases, there is a
UI library (a facade, if you will) that puts things together and allows for
inversion of control without stacking up code in the actual ASPX pages
and/or controls. I put the hard working bits in the business layer (and some
moving bits in the data layer), feeding the actual repository classes so I
can easily create simple mocks. Very testable and it makes it easy to swap
out user interfaces. Following this method, there is no need to call from
control to control, as the actual work is in classes in the libraries used
by the site.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top