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
Solved

execute code in a master page

Posted on 2012-03-23
18
430 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
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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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