Azhar
Azhar

Reputation: 20670

How to change the value of a control in a master page?

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.

Upvotes: 2

Views: 1797

Answers (1)

djdd87
djdd87

Reputation: 68456

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";

Upvotes: 4

Related Questions