Solved

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

Posted on 2014-03-07
8
1,584 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
  • 4
  • 4
8 Comments
 

Author Comment

by:ErinTheCoderGirl
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:ErinTheCoderGirl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now