Solved

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

Posted on 2014-03-07
8
1,626 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

822 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