Solved

execute code in a master page

Posted on 2012-03-23
18
426 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.net mvc foreach 3 33
DataGridView Events ? 3 37
Turn on intranet settings 1 36
Reset asp.net login password 4 26
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

17 Experts available now in Live!

Get 1:1 Help Now