Solved

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

Posted on 2014-03-07
8
1,643 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
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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