How to use a USERCONTROL to call a LABEL

G

Guest

I created a USERCONTROL and i want to call a LABEL from an ASPX page.
The USERCONTROL is in the ASPX page
I have the label :-
Label lblstr = new Label();
And displaying using:-
lblstrp.Text=lblstr.Text;
<asp:label id="lblstrp" BorderColor="#ff3333" BorderStyle="Dashed"
BorderWidth="3" ForeColor="#0033cc"
Font-Size="25" Runat="server" Width="55%" BackColor="#ffff00"></asp:label>

I have a user control" header.ascx".
I want to place the usercontrol in the ASPX page using for example
<HEADER:topheader runat="server"></HEADER:topheader>

Any ideas?
 
C

Craig Deelsnyder

I created a USERCONTROL and i want to call a LABEL from an ASPX page.
The USERCONTROL is in the ASPX page
I have the label :-
Label lblstr = new Label();
And displaying using:-
lblstrp.Text=lblstr.Text;
<asp:label id="lblstrp" BorderColor="#ff3333" BorderStyle="Dashed"
BorderWidth="3" ForeColor="#0033cc"
Font-Size="25" Runat="server" Width="55%"
BackColor="#ffff00"></asp:label>

I have a user control" header.ascx".
I want to place the usercontrol in the ASPX page using for example
<HEADER:topheader runat="server"></HEADER:topheader>

Any ideas?

I'm not sure I understand, your post is a little confusing. But to answer
the question posed in the subject, the usercontrol has a .Page property,
from which you can call FindControl to get the control by it's id (you'll
have to cast the result to a Label).

However, IMHO, this is not a good design. The usercontrol should not know
about controls in the page containing it, because that limits the
usefulness of the usercontrol as it can only be used in a page that has
the label. However, you could have your page inherit from a base class
that informs the usercontrol of the label (perhaps as a property). Of
course then your usercontrol still can only go inside these types of page.

Otherwise user controls should be black boxes that communicate and are
affected by its properties and events being set or handled by pages; the
page pulls or sets info on the usercontrol while also listening for events
from it...

just my opinion...
 
G

Guest

Thx Craig for the response!
What i really want to do is to place the label below in a USERCONTROL:-

<asp:label id="lblstrp" BorderColor="#ff3333" BorderStyle="Dashed"
BorderWidth="3" ForeColor="#0033cc"
Font-Size="25" Runat="server" Width="55%"
BackColor="#ffff00"></asp:label>

And then call it on my ASPX page.
Hope its clear now.
 
B

Brock Allen

You need to make a public property available from your user control that
in turn sets the text on the label.

ASCX:
<%@ Control %>
<script runat=server>
public string Text
{
get {return _myLabel.text;}
set {_myLabel.Text = value; }
}
</script>
<asp:Label runat=server id=_myLabel %>

Then in the ASPX:
<uc:YourUC runat=server Text="Some Text" />
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top