User Controls accessing outside objects

T

tshad

Is there a way for a User Control to access an object (such as label or
textbox) on the .aspx page that calls it?

For example:

x.aspx
**************************************
....
Sub Page_Load(s as Object, e as EventArgs)
LoadControl("x.ascx")
End Sub
....
<asp:Label ID="theLabel" runat="server"/>
**************************************

x.ascx
**************************************
Sub Page_Load(s as Object,e as EventArgs)
theLabel.Text = "Some Text"
End Sub
****************************************

This would get me the error:

Name 'MessageOutside' is not declared

I know how to access methods and properties in the User Control from the
..aspx page, but not the other way around.

Thanks,

Tom
 
M

michaelkb

The FindControl method I was talking about in the other thread....

Label lbl = (Label)Page.FindControl("controlidhere");

or

Label lbl = (Label)Parent.FindControl("controlidhere");
 
T

tshad

The FindControl method I was talking about in the other thread....

Label lbl = (Label)Page.FindControl("controlidhere");

or

Label lbl = (Label)Parent.FindControl("controlidhere");

That looks like exactly what I am looking for.

But it doesn't seem to be working or at least it doesn't seem to be getting
called.

I have a control that calls this control.

**************************************
Dim pageInit as Control = LoadControl("/controls/pageInit.ascx")
if pageInit is nothing then
trace.warn("pageInit is nothing")
else
trace.warn("pageInit is not nothing")
end if
********************************************

It seems to be loading as the tras I get is "pageInit is not nothing".

but the code doesn't seem to be working. The code is in the Page_Load
section.

The pageInit.ascx is:
**************************************************
<script runat="server">
Sub Page_Load(s as Object, e as EventArgs)
trace.warn("Inside pageInit.ascx")
Dim UserLoggedOnLabel as Label =
CType(Page.FindControl("UserLoggedOnLabel"),Label)
UserLoggedOnLabel.Text = "this is a test"
if not session("User") is nothing then session("LastPageVisited") =
Session("User").LastPageVisited
End Sub
</script>
**************************************************

The Page_Load in this control doesn't seem to be running.

I don't get the Trace message, the UserLoggedOnLagel doesn't seem to get
changed and the Session("LastPageVisited") doesn't get set.

If I don't have the code you mentioned, I get the error, so I assume the
control is getting loaded.

Am I missing something here?

Thanks,

Tom
 
M

michaelkb

I'd put a breakpoint somewhere in the mix and start looking at yoru
controls and controls within those controls and find the one you are
looking for in the QuickWatch window. Start with Page.Controls, see how
many there are, and klink around until you find the right path.
Eventually, you will get to the control you are trying to find with
Page.FindControl. Take note of the path you've taken to that point,
(the one that reflects at the top of the quickwatch window).

This is what I do when I get lost with my controls and need to find out
where in the hell one is in the hierarchy :)
 
T

tshad

I found out what the problem was. Just not sure why

Apparently, if you do a LoadControl only, it won't run the Page_Load
function:

Dim pageInit as Control = LoadControl("/controls/pageInitialization.ascx")

but as soon as I added it to a PlaceHolder object, it worked fine.

PageUserControl2.Controls.Add(pageInit)

Where I am confused is that I was under the impression that you didn't have
add a control to a PlaceHolder but if you didn't it would just put it on the
page somewhere. If you wanted to control were it was placed you had to
create a PlaceHolder and add it to the Controls group.

Also, I tried your FindControl code and it works fine. If you use Page, it
will find the control if it is on the .aspx page.

In my example, I was having the .aspx call a control (.ascx) which calls the
other control (.ascx). I was doing the FindControl from the 2nd control and
the control I was looking for was in the 1st control.

..aspx
---.ascx
----UserLoggedOn
----.ascx
--------Label lbl = (Label)Page.FindControl("controlidhere");

It wasn't finding it. I assume it was because the ID is
"_ctl1_UserLoggedOn" on the html page and _ctl1:UserLoggedOn in my Trace.

But if I change it to your other FindControl:

Label lbl = (Label)Parent.FindControl("controlidhere");

This finds it fine. I assume this is because it knows to strip the Control
prefix (or it adds the prefix to the search value).

Is there a way to just say FindControl to anywhere on the page?

Thanks for all the help,

Tom
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top