Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

execute code in a master page

Posted on 2012-03-23
18
Medium Priority
?
444 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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