We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

brgdotnet
brgdotnet asked
on
Medium Priority
532 Views
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>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainTitle">
	<uc:ClientMenu runat="server" ID="clientMenu" />            
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="Content">
    <asp:ContentPlaceHolder runat="server" id="Content" />
</asp:Content>
 
Below is the label control inside of my web user control ClientMenus.ascx :
 
    <asp:Label ID="amStatus" runat="server" ForeColor="#FF3300" Text="Status" 
        Visible="False"></asp:Label>

Open in new window

Comment
Watch Question

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

Open in new window

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

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
brgdotnetcontractor

Author

Commented:
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
Albert Van HalenAnalyst developer
CERTIFIED EXPERT
Commented:
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

Albert Van HalenAnalyst developer
CERTIFIED EXPERT

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

Open in new window

Use the C# version I gave you not the VB.NET version as you are coding in C#
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.