473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User Controls accessing outside objects

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
Aug 3 '06 #1
4 1676
The FindControl method I was talking about in the other thread....

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

or

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

tshad wrote:
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
Aug 3 '06 #2
<mi*******@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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
>
tshad wrote:
>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

Aug 3 '06 #3
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 :)

tshad wrote:
<mi*******@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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

tshad wrote:
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
Aug 3 '06 #4
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
<mi*******@hotmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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 :)

tshad wrote:
><mi*******@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegr oups.com...
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
>
tshad wrote:
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

Aug 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1705
by: Tony Hedge | last post by:
Hello, I'd like to reference a control in a user control that is not in the user control. For esample, if I had the following in my .aspx: .... <html> <form ID="frmMain"....> ... <asp:Label...
5
1696
by: Jonathan Williams | last post by:
Hi, I have an object which inherits from WebControl (CUSTOM : WebControl) In this object I have code in which I add child contols: protected override void CreateChildControls() {...
2
3006
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
3
1759
by: tshad | last post by:
I am trying to put together a user control (.ascx). Can you access an outside control from it? My control: login2.ascx ****************************************************************...
0
925
by: tshad | last post by:
I have a User Control with 4 Textboxes. I want to be able to access the properties (.text, .visible, .enable etc) directly from outside the Controls as the normal properties (.text, .visible...
4
7577
by: Eric | last post by:
I got a particular problem in visual studio 2005 There's a user control on page and I want to meka a cast like this MyPage mp=(MyPage)this.Page; Error is : cannot cast from...
1
1859
by: =?Utf-8?B?SlA=?= | last post by:
I have a default.aspx page. Default.aspx has placeholders. ASCX user controls are loaded into these placeholders at Page_Load. Each user control has a MultiView, within that there are two views...
4
1402
by: Jenni.Haughton | last post by:
I have a solution with 2 projects in it. I need to programatically add a user control from one project into the other one (as you cannot declare this normally on the form because it is in a...
9
2494
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.