2010-02-02 11 views
7

Ho masterpage.master.vb dove ho proprietà, come;Accesso alle proprietà della pagina principale da pagine figlio in ASP.net VB

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 

Quindi ho una pagina di aspx in cui ho bisogno di utilizzare questa proprietà, ad esempio;

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

Qualcuno può darmi un'idea su come procedere? Ho provato a cercare ma la maggior parte degli articoli parla nel contesto dei controlli web ...

Grazie.

risposta

7

Qui si va:

How to: Reference ASP.NET Master Page Content

Dall'articolo, sembra che

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

dovrebbe funzionare per voi.

Basta assicurarsi di aggiungere la direttiva MasterType come descritto o si potrebbe ottenere un errore di conversione del tipo. (Oppure potresti usare una variabile del tuo tipo di pagina master invece di Master, come suggerisce daRoBBie nella sua risposta.)

Ho creato un sito Web di prova solo per testarlo e funziona. Ecco il sorgente completo del sito:

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 

+0

Questo è quello che stavo cercando prima di fare la mia domanda, ma senza fortuna! ... ottengo; SQLError non è un membro di system.web.ui.masterpage. – Phil

+0

Hai aggiunto la direttiva MasterType? Sembra che potrebbe essere il tuo problema. La proprietà Master non è fortemente tipizzata, quindi è di tipo System.Web.UI.MasterPage, che non ha la tua proprietà. –

+0

Ho <% @ MasterType VirtualPath = "~/my.master" "%> e in @page MasterPageFile =" ~/my.master " – Phil

3

si può lanciare il masterpage al tipo corretto:

MyMasterPageType m = (MyMasterPageType)Master; 

Quindi è possibile accedere alle proprietà:

m.SqlError 

Se si dispone di più pagine master, lasciare ereditare tutte le proprie pagine master da un'interfaccia e trasmettere la pagina master a tale interfaccia.

1

È possibile utilizzare anche <% @ MasterType% per questo.

0

Se hai seguito i passaggi nella risposta di Andy West e hai uno o più errori di compilazione che leggono: Foo is not a member of 'System.Web.UI.MasterPage', assicurati che siano l'unico errore di compilazione nel tuo elenco.Se sono presenti altri errori di compilazione che devono essere corretti, è necessario correggerli prima di continuare a risolvere il problema con lo MasterPage.

Questi altri errori di compilazione potrebbero impedire al compilatore di analizzare il tuo MasterPage e di rilevare le proprietà aggiuntive. Una volta risolti, esegui una ricompilazione completa.

Problemi correlati