How to reference an object in a user control from its parent page

M

moondaddy

I have a page which has a user control called CheckOutStatusBar and it lives
in a table call like this:

<%@ Register TagPrefix="uc1" TagName="CheckoutStatusBar"
Src="Navigation/CheckoutStatusBar.ascx" %>
html.....
<td>
<uc1:checkoutstatusbar id="CheckoutStatusBar1"
runat="server"></uc1:checkoutstatusbar>
</td>



I need to change the formating of a link button in CheckoutStatusBar.ascx
from its parent page. How an I get a reference to it from the parent page?

Thanks.
 
S

Sergey Poberezovskiy

All you do is put a declaration into the page:

Protected WithEvents CheckoutStatusBar1
As "ProjectName"."UserControlClassName"

and then it becomes available to you in code.

hope this helps
 
N

Natty Gur

Hi,

CheckoutStatusBar oCheckoutStatusBar1 =
FindControl("CheckoutStatusBar1");

but you need to expose public or internal function from your user
control that will handle link formating change.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
M

moondaddy

Thanks that was perfect.

--
(e-mail address removed)
Sergey Poberezovskiy said:
All you do is put a declaration into the page:

Protected WithEvents CheckoutStatusBar1
As "ProjectName"."UserControlClassName"

and then it becomes available to you in code.

hope this helps
 
J

John Saunders

moondaddy said:
I have a page which has a user control called CheckOutStatusBar and it lives
in a table call like this:

<%@ Register TagPrefix="uc1" TagName="CheckoutStatusBar"
Src="Navigation/CheckoutStatusBar.ascx" %>
html.....
<td>
<uc1:checkoutstatusbar id="CheckoutStatusBar1"
runat="server"></uc1:checkoutstatusbar>
</td>



I need to change the formating of a link button in CheckoutStatusBar.ascx
from its parent page. How an I get a reference to it from the parent
page?

It's best if you treat user controls like class instances (which they are).
This means that a user control shouldn't be exposing its link buttons to
the outside world. Instead, the user control should expose a method or
property which allows the link button formatting to change. For instance, in
the user control:

private bool _linkUnderlined = true;
public bool LinkUnderlined
{
get {return _linkUnderlined;}
set
{
_linkUnderlined = value;
if (_linkUnderlined)
{
lnkLinkButton.Styles["text-decoration"] = "underlined";
}
else
{
lnkLinkButton.Styles["text-decoration"] = "none";
}
}
}
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top