2

How to change the value of a control e.g. Literal in a user control and that User control is in master page and I want to change the value of that literal from content page.

((System.Web.UI.UserControl)this.Page.Master.FindControl("ABC")).FindControl("XYZ").Text = "";

Here ABC is user control and XYZ is Literal control.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Azhar
  • 20,500
  • 38
  • 146
  • 211

1 Answers1

4

The best solution is to expose the values through public properties.

Put the following into your ABC control that contains the XYZ control:

public string XYZText
{
    get
    {
        return XYZControl.Text;
    }
    set
    {
       XYZControl.Text= value;
    }
}

Now you can expose this from the Master page by adding the following property to the MasterPage:

public string ExposeXYZText
{
    get
    {
        return ABCControl.XYZText;
    }
    set
    {
       ABCControl.XYZText = value;
    }
}

Then to use it from any content page, just do the following (where MP is the MasterPage class):

string text = ((MP)Page.Master).ExposeXYZText;
((MP)Page.Master).ExposeXYZText = "New Value";
djdd87
  • 67,346
  • 27
  • 156
  • 195