Manipulating a User Control from another User Control

  • Thread starter Thread starter Tom Rowton
  • Start date Start date
T

Tom Rowton

This one has me a bit confused and I'm not finding what I
need in the MSDN or by searching these forums, so here
goes...


I have a rather large, complex code-in-page WebForm
(don't ask) and a section of that Form is 4 or 5
ASP:Panels pretending to be a set of Tabs, each with its
own section of the form.

In order to simplify managing this page, since code-
behind is not an option (don't ask, not my fault), I
thought I had the solution...

I would convert each Panel into a User Control.
The problem is that the panels usually depend on at least
one Form Control from at least one other Panel.

I already know about RaiseBubbleEvent() and OnBubbleEvent
() and have used them elsewhere, but they only work going
UP the container heirarchy.

I need to :
1 - (partially) handle an event in the UserControls
(panels), then (partially handle the same event in
ANOTHER UserControl(panel) on the same form/page.

2 - handle an event in a UserControl(panel) BUT be able
to retrieve values from form fields(Controls) in OTHER
UserControls(panels) to help determine how to handle the
event.

3 - (partially) handle an event in the Form(aspx)
depending on values of form controls in the UserControls

4 - manipulate values of form controls in the
UserControls


Like I said, I already know how to Bubble events, but
that only goes up.
The part I am missing is how to "Elbbub" events (bubble,
backwards) or manipulate data in child objects.

The trick is, this is all Code-in-Page and nothing will
convince those in control to return to Code Behind.

Anyone know how to do what I want/need to do?

I have a set of aspx/ascx pages that I can email or ftp
for anyone interested.
 
Not sure what you mean by "codebehind is not an option." It SOUNDS like what
you mean is that compiled CodeBehind DLLs are not an option. You can still
use CodeBehind, just put it in the same file as the Page Template. You can
put it right at the top of the Page, in a "runat=server" script. Example:

<%@ Page language="C#" %>
<script runat="server" >
public void Page_Load()
{
// Page_Load code goes here
}
</script>
<html>
....Page Template HTML and Code Goes here
</html>

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Not sure what you mean by "codebehind is not an
option."

By "code-behind", I am referring to the "strict"
definition of code-behind, i.e. a two-file page - *.as*x
+ *.as*x.vb.

I am using single-file or code-in-page pages, similar to
old-style ASP 3.0.

It sounds like maybe you know how to do this with
the "normal" MS-promoted code-behind setup, so if this is
easily converted to a single-file, code-in-page page,
please share the love. ;) I've found plenty of articles
on how to get/set values in User Controls from a page
that contains them using code-behind, but nothing about
doing this from one user control to another with single-
file pages.

I've been banging on this for two days now and can't seem
to retrieve info from one user control with code in
another without Bubbling the event to the parent page
(which works fine, but will only further complicate the
code).
 
Hi Tom,

I already DID "share the love," but apparently you didn't notice. Let me
reproduce it for you:

<%@ Page language="C#" %>
<script runat="server" >
public void Page_Load()
{
// Page_Load code goes here
}
</script>
<html>
....Page Template HTML and Code Goes here
</html>

Note the server-side script at the top. All you need to do is put the code
that would normally go in the CodeBehind class definition in a separate file
inside the CodeBehind script at the top of the page. I've already added the
Page_Load script as an example.

You definitely DON'T want to try programming ASP./Net with the old ASP
style. It's not designed for that, and will cause you beaucoups trouble if
you try to work with this Object-Oriented technology in a procedural way.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Hi Tom,

Yes, post your sample code. I, for one, am having a hard time understanding
the exact nature of the problem.

I understand your confusion re my use of the term "CodeBehind." There are a
number of different configurations for implementing what is often referred
to as "CodeBehind code," and this has a tendancy to muddy the water when
discussing how to implement the Class code that completes the Page Template
code. Taken literally, "CodeBehind" does refer to a separate file. I (and
some others) tend to use the term to talk about the "Code Behind the Page,"
which, of course, can be in a separate file or in the same file as the Page
Template (as we've been discussing). In terms of functionality and
optimization, it makes very littel difference whether you use a separate
file, DLLs, or just a single file with the Page Template and Page Class Code
in it.

The most important point, which you seem to understand, is that the code in
the Page Template (between the <html> and </html> tags) have as little
executable code in it as possible, and that you do your manipulation of the
Page through the CodeBehind Class. And I wouldn't be embarrassed to say that
I was including the "CodeBehind" code in the same file as the Page Template.
That is perfectly legitimate. I believe that Microsoft is considering taking
a similar approach in the future with the next version of ASP.Net. It
doesn't necessarily make sense to have ALL of the Pages in a project
compiled into a single DLL, especially when the first request for the page
results in the code being compiled and cached.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Okay, let me see if I understand you correctly. You've got an Event Handler
for one UserControl that needs to do something with another UserControl,
right? It looks to me like your error is in using Page.FindControl(...). The
FindControl() method of a Control looks for controls in that Control's
Controls Collection (was that a confusing sentence or what?). Your
UserControls are not in the Page's Controls Collection; they are in your
Form's Controls collection. Does that help?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Form's Controls collection. Does that help?
It's good info to have, but doesn't really fix my
problem.

in MyCaseSub.ascx where I have commented out
---------------------------------------------
Dim MyControl as Control =
Page.FindControl("MainPanel1")
if (not MyControl is Nothing) then
lblParent.Text =
MyControl.MainLabelValue()
else
lblParent.Text = "MainPanel1 not
found"
end if
------------------------------------------------

The "else" never happens.
MyControl is _never_ Nothing.
I can print the ID out just fine.
But I am unable to access the "MainLabelValue()" Property
GET - which works just fine from the bubbled event in
MyCase.aspx.

To sum up, I know that Page.FindControl() finds the
usercontrol, because I can print out the ID I've given it
in the HTML, but I cannot access the Property I've given
in the script block of the UserControl.

Or did I just misunderstand your point?
 
Back
Top