MasterPage Method Invocation from ContentPage

  • Thread starter Thread starter User
  • Start date Start date
U

User

OK, I asked in a reply, but I've wasted far too long so I'm going to put
a new post out there in hopes that it will be more visible.

I have a MasterPage. I want to call a method declared in that class from
a Page that uses the MasterPage. How do I do that?

public partial class List : System.Web.UI.MasterPage
{
protected ArrayList m_SortList;

public int AddSortChoice( string inString )
{
return m_SortList.Add( inString );
}
}


public partial class PostingList : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// this.Master.AddSortChoice("test"); /* <-- doesn't work */
}
}

I've tried:

- this.Master.AddSortChoice("up");
- UserControl ctl = (Master)this.Master;
- A bunch of other zany attempts
- Googling for call method in masterpage from contentpage

Any help would be greatly appreciated. Thanks.
 
Steve said:

Thanks Steve for the reply... but I don't think that's the same. I want
to call a method on the MasterPage, not pass data to the ContentPage.
Your solution is going the wrong way.

In my instance, I have a generic master page for lists. I want to have a
sorter control on that page. Instead of loading data from the
ContentPage to the master page (or finding the control), I want to be
able to call the method AddSortChoice("string") from the ContentPage
when it loads. That way, all the code for adding the choices to the
sorter only happens in one place. Am I making sense?

 
My bad...

Here's the solution:

1. Change the name of my MasterPage
2. Cast this.Master to the type of my MasterPage (MyList, for example)
3. Have fun.

Thanks Steve.
 
you need to cast the Page.Master property to your masterpage class
and call the method on it.

for ex:

((List)this.Master).AddSortChoice("test");

HTH,
<Ram/>
 
Hi,
I've been trying to acheive something similar. I have managed to invoke
methods within a masterpage from a page however I've had problems
trying to call such methods from a usercontrol.

Ideally I'd like to be able to call master page methods from user
controls with no code required in the page.

Will.
 
Back
Top