How to access and set a label control value in a web user control, from code that references it?

Posted on 2009-02-10
Last Modified: 2012-05-06
I have a web form named Mickey.aspx which uses a master page named ClientMaster.Master. The master page uses a web user control named ClientMenus.ascx. Inside of that web user control is a label named "amStatus". In my C# code behind file for Mickey.aspx, I need to set the value for the labele control "amStatus". The text value will have different text messages assigned to it. So my question is how can I access the label control "amStatus" from my code behind file Mickey.aspx.cs?
Here is the master page code for ClientMaster.Master :

<%@ Master MasterPageFile="~/Master/SiteTemplate.Master" Language="C#" AutoEventWireup="true" CodeBehind="ClientMaster.Master.cs" Inherits="ScramNet.Web.Clients.ClientMaster" %>

<%@ Register TagPrefix="uc" TagName="ClientMenu" Src="~/Controls/Clients/ClientMenu.ascx" %>

<asp:Content ID="ContentHead" runat="server" ContentPlaceHolderID="head">


<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainTitle">

	<uc:ClientMenu runat="server" ID="clientMenu" />            


<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="Content">

    <asp:ContentPlaceHolder runat="server" id="Content" />


Below is the label control inside of my web user control ClientMenus.ascx :

    <asp:Label ID="amStatus" runat="server" ForeColor="#FF3300" Text="Status" 


Open in new window

Question by:brgdotnet
    LVL 16

    Expert Comment

    Please try this
    CType(CType(Page.Master, ClientMaster).FindControl("ClientMenus").FindControl("amStatus"), Label).Text = "Status"

    Open in new window

    LVL 16

    Accepted Solution

    or in c#
    ((Label)((ClientMaster)Page.Master).FindControl("ClientMenus").FindControl("amStatus")).Text == "Status";

    Open in new window

    LVL 2

    Author Comment

    Hi Sunithnair. I get these error messages if I try and get the value using your recomendation. Can you spend more time helping me to figure this out?
    Here are the error messages I get:

    Error      5      Only assignment, call, increment, decrement, and new object expressions can be used as a statement      C:\Dev\Customer Website\branches\SCRAM3\src\ScramNet\ScramNet.Web\Controls\Filters\ClientGeneralFilter.ascx.cs      55      17      ScramNet.Web

    Error      6      The type or namespace name 'ClientMaster' could not be found (are you missing a using directive or an assembly reference?)      C:\Dev\Customer Website\branches\SCRAM3\src\ScramNet\ScramNet.Web\Controls\Filters\ClientGeneralFilter.ascx.cs      55      27      ScramNet.Web

    Or with CType I get the following error message:

    Error      5      The name 'CType' does not exist in the current context      C:\Dev\Customer Website\branches\SCRAM3\src\ScramNet\ScramNet.Web\Controls\Filters\ClientGeneralFilter.ascx.cs      57      17      ScramNet.Web
    LVL 19

    Assisted Solution

    by:Albert Van Halen
    Just create a public string LabelText with a getter and setter in the codebehind of the ClientMenu usercontrol.
    The get method will return the value of the label, the set method will set it.

    That way, if you include the usercontrol in the masterpage you have an extra property which you can set : LabelText.

    <uc:ClientMenu runat="server" ID="clientMenu" LabelText="Whatever" />
    public string LabelText
        get { return amStatus.Text; }
        set { amStatus.Text = value; }

    Open in new window

    LVL 19

    Expert Comment

    by:Albert Van Halen
    Or if you want to do it in codebehind of the masterpage use this :
    clientMenu.LabelText = "Your label";

    Open in new window

    LVL 16

    Expert Comment

    Use the C# version I gave you not the VB.NET version as you are coding in C#

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now