Solved

execute code in a master page

Posted on 2012-03-23
18
438 Views
Last Modified: 2012-03-26
can code be executed in a master page?    I have this, do it does not execute:

Public Class Tabor
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MsgBox("asdasd")
    End Sub


End Class
0
Comment
Question by:HLRosenberger
[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
  • 10
  • 8
18 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758076
Yes you can execute code on a master page, however the code executes on the server-side so visual display elements like msgbox won't work, try this instead which will setup a javascript alert to occur on the page :

ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('type your message here');</script>");

Open in new window

0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758217
I understand that.  I'm just used the MsgBox as an example.  However, I put a break point at page load (using Visual Studio) and I do not get the break.  I don;t know what is wrong.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758261
Make sure you have set AutoEventWireup to true at the top of the master page definition

<%@ Master Language="VB.Net" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Main.master.vb" Inherits="MasterPages_Main" %>

Open in new window


Also how are you referencing the master page from the child page, should be something like this :

<%@ Page Title="Some Title" Language="C#" MasterPageFile="Main.Master" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" %>

Open in new window

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758283
Visually, I see my control (tree control) that is on the master page.   I need to load it with data from a database.   Ah, do I need the CodeBehind keyword in the master apsx?

This was a page create using a third party tool,
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758294
The CodeFile keyword is used to reference your master page code behind page from the aspx:

<%@ Page Title="Some Title" Language="C#" MasterPageFile="Main.Master" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" %>
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758340
I confused.  Does that statement go in the master.aspx, or the child aspx?  

If I put it in the master.aspx, it tell mes it the directive "page" is unknown.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758346
I have this in master.aspx.

%@ Master Language="vb"  AutoEventWireup="true" CodeBehind= "~/Master Pages/Tabor.master.vb" %>

Still, I do not get the page_init.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758354
Whoops sorry cut and paste error :

<%@ Master Language="VB.Net" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Main.master.vb" Inherits="MasterPages_Main" %>
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758389
Change to :

<%@ Master Language="vb"  AutoEventWireup="true" CodeFile="Tabor.master.vb"  Inherits="Tabor"%>

The Inherits Keyword needs to be set to your class name in the codebehind file.
You should also set the class as a partial class in the codebehind like:

Partial Class Tabor

---
End Class
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758462
I appreciate your help.

arggg.....  still not working....

here's my class in the  code behind:

Partial Class Tabor

    Inherits System.Web.UI.MasterPage
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

And I added it to the Master directive:

<%@ Master Language="VB"  AutoEventWireup="true"  Inherits="Tabor" CodeBehind= "Tabor.master.vb" %>

But now Visual Studio tells me it cannot load type "Tabor"





End Class
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758479
Build the project from the Build menu that should get rid of the cannot load type error.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758492
I did.  No change.  Something is wrong somewhere.  Maybe a typo I cannot see?
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758555
Try closing and reopening both the master and the master.vb files might just be visual studio not updating it's cache properly
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 37758593
let me ask this - THis is a non-Visual Studio project.   It a third party tool.  Where does Visual Studio look for classes?  Is there more to it than Inherits="Tabor"?  Do I need to fully qualify the path/name?
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 37758611
Think I spotted the problem you are still using the codebehind keyword but it needs to be the CodeFile keyword :

change :
<%@ Master Language="VB"  AutoEventWireup="true"  Inherits="Tabor" CodeBehind= "Tabor.master.vb" %>

to
<%@ Master Language="VB"  AutoEventWireup="true"  Inherits="Tabor" CodeFile= "Tabor.master.vb" %>
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37758620
For some reason it's a different keyword for master pages not sure why.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 37758674
That was it!!!  Thanks so much!!

So, what the difference between codefile and codebehind?
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37765658
Seems CodeBehind was .Net 1.1, and was changed to CodeFile in .Net 2.0+
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

626 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