views:

103

answers:

3

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?

A: 

You should put it in the masterpage if you can. Ideally, your content page should have no knowledge of the parent and should assume that it might be used anywhere.

This will lead to a more componentized, reusable design for your content pages.

womp
+5  A: 

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();
Joel Potter
Good solution. You would need to cast this.Page to IMyTaggablePage in order to call the method.
apathetic
Thanks. Missed that.
Joel Potter
This would require you master page to know about content pages where if you made the content page send data to the master, your master wouldn't care about how many possible content pages you have or what they do. You would just expose the interface to have content pages customize the master page.
Kelsey
Excellent example. Allows the masterpage to derive info about the page from the child, yet doesn't unnecessarily couple the masterpage or children.
Jagd
@Kelsey: Both approaches are valid, but I prefer treating the master as the observer of the child, hence the child doesn't interact with the master. This way, you could drop in a nested master page without changing a thing and they could both access members of the page.
Joel Potter
A: 

You can expose functionality in your master page via your content page by adding the following to you content pages aspx file:

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

Then in your content page you should be able to access any methods you exposed from your master page.

So you master page would have a method like:

public void BuildMyCustomStuff(YouInputType in)
{
    // Do something with the data passed in
}

Then in your content page you would call the function with:

Master.BuildMyCustomStuff(dataIn);
Kelsey