Access homepage properties from child pages in ASP.net VB

I have masterpage.master.vb where I have properties such as;

Private _SQLerror As String Public Property SQLerror() As String Get Return _SQLerror End Get Set(ByVal value As String) _SQLerror = String.Empty End Set End Property 

Then I have an aspx page in which I need to use this property, for example:

  If **[masterpage property sqlerror]** = Nothing Then InternalSQLErrLabel.Text = ("No Errors Reported") End If 

Can someone give me an idea how to do this? I tried searching, but most articles are discussed in the context of web controls ...

Thanks.

+7
properties master-pages
source share
4 answers

Here you go:

A practical guide. Link to the ASP.NET Home Page

From the article it looks like

 If Master.SQLerror = Nothing Then InternalSQLErrLabel.Text = ("No Errors Reported") End If 

should work for you.

Just remember to add the MasterType directive as described, or you may get a type conversion error. (Or you can use a variable like the main page instead of Master, as daRoBBie suggests in your answer.)

I created a test website to test this and it works. Here is the full source of the site:

Site1.Master

 <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebApplication1.Site1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> This is the Master Page content. <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html> 

Site1.Master.vb

 Public Partial Class Site1 Inherits System.Web.UI.MasterPage Private _SQLerror As String Public Property SQLerror() As String Get Return _SQLerror End Get Set(ByVal value As String) _SQLerror = String.Empty End Set End Property End Class 

WebForm1.aspx

 <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %> <%@ MasterType VirtualPath="~/Site1.Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> This is the Content Page content. <asp:Label ID="InternalSQLErrLabel" runat="server" Text="Label"></asp:Label> </asp:Content> 

WebForm1.aspx.vb

 Public Partial Class WebForm1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Master.SQLerror = Nothing Then InternalSQLErrLabel.Text = ("No Errors Reported") End If End Sub End Class 

+7
source share

you can assign the master page to the correct type:

 MyMasterPageType m = (MyMasterPageType)Master; 

Then you can access your properties:

 m.SqlError 

If you have multiple master pages, let all your master pages be inherited from the interface and drop the main page to that interface.

+3
source share

You can also use <% @ MasterType%>.

+1
source share

If you followed the steps in Andy West's answer and received one or more compilation errors: Foo is not a member of 'System.Web.UI.MasterPage' , make sure that they are the only compilation error in your list. If there are any other compilation errors that need to be fixed, they must be fixed before you continue troubleshooting MasterPage .

These other compilation errors may prevent the compiler from parsing your MasterPage and defining additional properties. Once they are resolved, perform a complete recompilation.

0
source share

All Articles