User control within User control within Master Page and setting a property c#

44 views Asked by At

How do I get/set a property within a User Control inside User Control within Master Page in c# ?

Example structure:

  • Web User Control 'WebUserControlA.ascx'
  • Web User Control 'WebUserControlB.ascx'
  • Master Page 'Content.master'
  • Web Form Page 'Test.aspx'

'Test.aspx', is a content page from the 'Content.master' page, it includes:

 <%@ MasterType VirtualPath="~/Content.master" %>

The content.master page includes the 'WebUserControlA.ascx' and in 'WebUserControlA.ascx' is 'WebUserControlB.ascx' included.

I wish to be able to set text of a label control in the 'WebUserControlB.ascx' from the 'Test.aspx' code behind file.

UPDATED NOTES:

After receiving a potential answer from 'VDWWD', which allowed me to set the text label, I need to expand my original question:

"I would like to control a get/set a property within the code behind of the WebUserControlB.ascx so that I'm able to display content from the database correctly, for example I want to pass in a country code from the code behind of test.aspx to the WebUserControlB.ascx code behind"

 public string CountryCode { get; set; }

I have tried the following however was not able to get hold of the get/set property:

 UserControl WebControlB = (UserControl)Page.Master.FindControl("WebUserControlA").FindControl("WebUserControlB");
 if (WebControlB != null) {
     WebControlB.CountryCode = "[CODE]";
 }

When trying to do this i get the following error:

'UserControl' does not contain a definition for 'CountryCode' and no extension method 'CountryCode' accepting a first argument of type 'UserControl' could be found (are you missing a using directive or an assembly reference?)

Thanks for helping

0

There are 0 answers