'Nesting' Controls and setting their public properties - C#

G

Gramps

Hi,


I have a simple ascx(Banner) that consists of an image a
label and a public property, 'ThisTitle'. From a page I
want to set the label via assignment to the property. The
only way I can find to set this property is in the HTML of
my page:

<uc1:Banner id="Banner1" runat="server"
ThisTitle="Login"></uc1:Banner>

and in the code-behind Page_Load of the ascx I have:

lbl.Text=this.ThisTitle.

This works and allows me to provide a common image banner
with page specific text.

1) Can I set the 'ThisTitle' property of ascx in code-
behind of my PAGE?

2) I have a second ascx which uses the first as a
constiuent control to provide greater common functinoality
to those pages that need it. From a page containing this
new 'bigger' ascx, how can I set 'ThisTitle' of the Banner
ascx that it contains? Again, I would prefer to do this
from the code-behind of the page.

I am using VS.Net IDE and C#

Thanks
 
K

Karl

Your page has this:
<uc1:Banner id="Banner1" runat="server" ThisTitle="Login"></uc1:Banner>

your code-behind should look like:

protected Banner Banner1;

you can do

Banner1.ThisTitle = "some value";


If you want to access that user control from a 2nd user control, its best to
go through the page, change the declaration to public:

public Banner Banner1;

and in your 2nd user control, access Banner1 from the page:

((WebForm1)Page).Banner1.ThisTitle = "some value";

Notice I have to cast Page to WebForm1 (which I'm assuming is the class of
your page...it probably isn't, but is good for our example). If you want to
do this across multiple pages, consider making those pages inherit from a
common base-page which exposes the banner, yuo can then do

if (Page is MyBasePage){
((MyBasePage)Page).Banner1.ThisTitle = "some value";
}


hope this helps,
Karl
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top