C# – the best approach to populate a masterpage control which is dependent on the content page

asp.netcmaster-pagesvb.net

I have a master page called SampleMaster.master and this page contains a Repeater control

The Repeater control will be used to display the relevant tags associated with each content page and the tags will vary between content pages

The data extraction method Tags.GetTags() is working but I do not know the best approach to populate the Repeater control in the master page dependent on what the content page is.

Would the code reside in the masterpage page code behind or the content page code behind?

Best Answer

I would suggest exposing a property or method on the child page which hands the master page the tags to display. For example

partial class myPage : IMyTaggablePage
{
    // the following is an interface member
    public List<string> GetTags()
    {
        return this.Taglist; // assuming taglist was populated somewhere on this page.
    }
}

Then on your master page you can write something like:

if (this.Page is IMyTaggablePage)
    var tags = (Page as IMyTaggablePage).GetTags();
Related Topic