Accessing masterpage properties from child pages in ASP.net VB
Asked Answered
I

4

8

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 in, such as;

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

Can anyone give me an idea how to go about this? I've tried searching but most articles talk in the context of web controls...

Thanks.

Inaudible answered 2/2, 2010 at 7:35 Comment(0)
V
8

Here you go:

How to: Reference ASP.NET Master Page Content

From the article, it looks like

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

should work for you.

Just be sure to add the MasterType directive as described or you might get a type conversion error. (Or you could use a variable of your master page type instead of Master, as daRoBBie suggests in his answer.)

I have created a test Web site just to test this out, 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

Valorievalorization answered 2/2, 2010 at 7:50 Comment(6)
Thats what I was trying before asking my question, but with no luck! ... i get; SQLError is not a member of system.web.ui.masterpage.Inaudible
Did you add the MasterType directive? It looks like that might be your problem. The Master property is not strongly typed, so it is of type System.Web.UI.MasterPage, which does not have your property.Valorievalorization
I have <%@ MasterType VirtualPath="~/my.master”" %> and in @page MasterPageFile="~/my.master"Inaudible
I built a test Web site to show that this works. Have a look. Not much more I can do than that.Valorievalorization
Thats really helpful thanks a lot. Ive now got it working and marked you as the best answer. Phil.Inaudible
If you do everything Andy says and it still doesn't work: Put the property in the masterpage, rebuild the project....then CLOSE Visual Studio. Open it back up, and try the content page part. I spent about 2 hours on this before remembering sometimes that can happen. AHHHH!Photomontage
B
3

you can cast the masterpage 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 masterpages inherit from an interface, and cast the masterpage to that interface.

Brookner answered 2/2, 2010 at 7:51 Comment(0)
B
1

You can use <%@ MasterType %> also for this.

Brassie answered 4/3, 2010 at 11:32 Comment(0)
C
0

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

These other compile errors may be preventing the compiler from parsing your MasterPage and detecting the extra properties. Once they are resolved, do a full recompile.

Chainsmoke answered 29/12, 2015 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.