Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set property of master page from content page and get that property value from another content page in asp.net

Tags:

c#

asp.net

I have a master page with one property name event type. Now I want to set this property from a content page and then that property value should be available to another content page.

Is this possible with asp.net? If yes then please help me out.

And yes, my content page already inherits another page which is not master.

like image 744
Darshan Avatar asked Mar 15 '13 11:03

Darshan


People also ask

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.

How can you reference a control on the master page from the content page?

To reference a control on the master pageUse the FindControl method, using the value returned by the Master property as the naming container. The following code example shows how to use the FindControl method to get a reference to two controls on the master page, a TextBox control and a Label control.

How do I connect a contents page to a master page?

In the Visual Studio installed templates list, click Web Form. Select the Select master page check box, and then click Add. The Select a Master Page dialog box appears. In the Contents of Folder box, click the master page that you want to associate with the page you are creating, and then click OK.


1 Answers

If this property is a custom property that you added to your master page, then you'll have to add a MasterType declaration to the page that is incorporating it.

<%@ MasterType 
    virtualpath="~/Path/To/Your.master" 
%>

This allows the web site or application to know the specific type of the master page at compile time and allows you to access it as you would any other property in a control.

Page.Master.MyCustomerProperty = someValue;

Edit: As a side note, in getting this property down to your next control, it would probably be best to create (and raise) a custom event indicating the property has changed. This way a number of controls can subscribe to the event and "self-update" without having to be concerned with the timing of when the property is set.

Example: In your master page you can define an event as a "global" variable. Then in your property you can raise this event.

public event EventHandler myPropertyChanged;
public delegate void MyPropertyChanged(object sender, EventArgs e);

//...

public string MyProperty
{
    get
    {
       return _myProperty;
    }
    set
    {
        _myProperty = value;
        if (myPropertyChanged != null)
            myPropertyChanged(this, new EventArgs());
    }
}

Then in your other controls, you can subscribe to this event to know when it changes:

protected void Page_Load(object sender, EventArgs e) 
{
    Page.Master.MyPropertyChanged += new EventHandler(MasterPropertyChanged);
}

protected void MasterPropertyChanged(object sender, EventArgs e) 
    //Rememeber you need the VirtualType in order for this event to be recognized
    SomeLocalValue = Page.Master.MyProperty;
}

A good step-by-step tutorial of this process can be found on CodeProject. A good c# custom events tutorial can be found on the MSDN.

like image 195
Joel Etherton Avatar answered Oct 01 '22 07:10

Joel Etherton