• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

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

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

0
brgdotnet
Asked:
brgdotnet
  • 3
  • 2
2 Solutions
 
sunithnairCommented:
Please try this
CType(CType(Page.Master, ClientMaster).FindControl("ClientMenus").FindControl("amStatus"), Label).Text = "Status"

Open in new window

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

Open in new window

0
 
brgdotnetcontractorAuthor 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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Albert Van HalenAnalyst developerCommented:
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

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

Open in new window

0
 
sunithnairCommented:
Use the C# version I gave you not the VB.NET version as you are coding in C#
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now