Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing masterpage 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 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.

like image 517
Phil Avatar asked Feb 02 '10 07:02

Phil


People also ask

How do you access master page controls from the content page?

To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master. SendDataToContentPageButton() method is used to acees master page button.

How will you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.

Which property can be used in Web user control to access the master page?

c# - Accessing masterpage property from a usercontrol - Stack Overflow.


2 Answers

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

like image 119
Andy West Avatar answered Sep 21 '22 08:09

Andy West


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.

like image 20
Rob Tillie Avatar answered Sep 22 '22 08:09

Rob Tillie