Solved

asp.net vb - exec a masterpage function in a content page?

Posted on 2014-03-07
8
1,671 Views
Last Modified: 2014-03-24
Hi - I'm using asp.net vb masterpages.

I want to have a function that sits in the masterpage, that is executed by a child content page, passing a string to it.

Any ideas how i can do this?
0
Comment
Question by:ErinTheCoderGirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 

Author Comment

by:ErinTheCoderGirl
ID: 39912028
This nearly does the job:
http://www.4guysfromrolla.com/articles/013107-1.aspx

it allows access from content page to a master page sub.

However, I'm using nested master pages, and it doesn't allow reference to the top level master page sub, when in a content page that belongs to a nested master page.

I keep having problems with nested master pages!

Any ideas how i can do this?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912108
Each page/master has a Master property so you can walk up the tree to the one you need.

You will of course need to know the type of the top-level master page in order to be able to cast it and call methods on it.

Take this example that calls a custom method in a master page from a WebForm, inside a nested master page:
'// In WebForm
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '// grab top-level master page
        Dim parent As MasterPage = Me.Master.Master

        '// cast to correct type and call method
        DirectCast(parent, Site1).UpdateLabel(Me.Label1, "Hello World")

    End Sub

Open in new window

0
 

Author Comment

by:ErinTheCoderGirl
ID: 39912249
Hi - do you know of a web article about this?

I've added your code, but just getting errors in vs2010:

Label1 is not a member of 'extra' - this being the content page aspx

Also, Type 'Site1' is not defined.

it also says that a reference to an assembly is required, not sure what that means..


Any ideas?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912352
I didn't mean for you to copy/paste that as is - it was merely intended to show the mechanics of finding the top-level master page and calling a method on it.

The bits you need to focus on are:
'// this line gets the master page two levels up from the webform
Dim parent As MasterPage = Me.Master.Master

Open in new window

And:
'// cast to correct type and call method
'// Here you replace 'Site1' with the type of your master page, and replace the 'UpdateLabel(Me.Label1, "Hello World")' part with a method in your master page that you want to call
DirectCast(parent, Site1).UpdateLabel(Me.Label1, "Hello World")

Open in new window

0
 

Author Comment

by:ErinTheCoderGirl
ID: 39912580
Hi - I'm a newbie, so please bare with me.

When you say Site1 with the type of master page, I'm not sure what a MasterPage type is...aren't they just master pages?    I can't see where I've declared a Type.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912613
If you open up the code page for your master page, you will see something like the following at the top:
Public Class Site1
    Inherits System.Web.UI.MasterPage

Open in new window

This is taken from the sample i've been playing around with. In this case Site1 is the type of my page, although it inherits from the base MasterPage type.

If you have a method in your master page, then you need to cast to its specific type in order to be able to call that method.
0
 

Author Comment

by:ErinTheCoderGirl
ID: 39913241
sorry - I'm not clever enough to implement your vague comments into code... I can't do it.

I'll try and rethink my design and scrap the bloomin' nested master pagers, or just have a cup of tea.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39917069
I've attached a small project that might make things clearer. It consists of a MasterPage (called Site1), a nested master page (called NestedMasterPage1) and a content page.

The content page calls a method in Site1 (the top-level master page) that sets the value of a label.
WebApplication1.zip
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question