?
Solved

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

Posted on 2014-03-07
8
Medium Priority
?
1,730 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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