[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • 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
 
brgdotnetAuthor 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Albert Van HalenCommented:
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 HalenCommented:
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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