User Control - setting properties

J

John Carnahan

Don't quite get it.

User control has a label in a html table and I want to set the text property
of the label from code behind. (MyLabel)

<%@ Register TagPrefix="MCP" TagName="TOPMENU" src="TopMenu.ascx" %>
<HTML>



FORM....
<MCP:TOPMENU id="TopMenu1" runat="server"></MCP:TOPMENU>


CODE BEHIND...PAGE LOAD

??? MyLabel.Text="Something"

Can't see the label object in the code behind... Why not? How do I get to
it?
 
S

Scott Mitchell [MVP]

User control has a label in a html table and I want to set the text property
of the label from code behind. (MyLabel)

<%@ Register TagPrefix="MCP" TagName="TOPMENU" src="TopMenu.ascx" %>
<HTML>


FORM....
<MCP:TOPMENU id="TopMenu1" runat="server"></MCP:TOPMENU>


CODE BEHIND...PAGE LOAD

??? MyLabel.Text="Something"

Can't see the label object in the code behind... Why not? How do I get to
it?

John, give the User Control a public string property, like MyLabelText.
Then in the Page_Load event of the User Control, set its Label's Text
property to the value of the MyLabelText property. In the ASP.NET Web
page's code behind class, set the User Control's MyLabelText like so:

TopMenu1.MyLabelText = "Foo"

(You'll need to have manually added in a reference to TopMenu1 in your
class, like:

Protected TopMenu1 as TopMenu
)

For more information, be sure to read:

An Extensive Examination Of User Controls
http://msdn.microsoft.com/asp.net/u...l=/library/en-us/dnaspp/html/usercontrols.asp

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
J

John Carnahan

Works great, thanks

Scott Mitchell said:
John, give the User Control a public string property, like MyLabelText.
Then in the Page_Load event of the User Control, set its Label's Text
property to the value of the MyLabelText property. In the ASP.NET Web
page's code behind class, set the User Control's MyLabelText like so:

TopMenu1.MyLabelText = "Foo"

(You'll need to have manually added in a reference to TopMenu1 in your
class, like:

Protected TopMenu1 as TopMenu
)

For more information, be sure to read:

An Extensive Examination Of User Controls
http://msdn.microsoft.com/asp.net/using/building/webcontrols/default.aspx?pu
ll=/library/en-us/dnaspp/html/usercontrols.asp

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

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,819
Latest member
masterdaster

Latest Threads

Top