Solved

execute code in a master page

Posted on 2012-03-23
18
425 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now