主页中的访问控制来自同一母版页的共享功能

时间:2013-02-26 11:27:12

标签: vb.net master-pages

我正在从内容页面调用母版页的共享功能。在该共享函数中,我想访问母版页中的控件,但我不知道如何。

main.master

<asp:Literal ID="ltCurrency" runat="server" />

main.master.vb

Partial Public Class main
Inherits System.Web.UI.MasterPage

Public Property CurrencyText() As String
    Get
        Return ltCurrency.Text
    End Get
    Set(ByVal value As String)
        If value <> "" Then
            ltCurrency.Text = value
        End If
    End Set
End Property

Public Shared Function DoSomething() As String  
    ltCurrency.Text="SOME TEXT" 'throws error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.    

    CurrencyText="SOME TEXT" 'this property isn't found at all

'我还尝试实例化当前母版页的新类: CTYPE(主,母版).CurrencyText

End Function


End Class

来自page1.aspx我打电话:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    main.DoSomething()
End Sub

我还能做什么?

2 个答案:

答案 0 :(得分:4)

为了它的价值(我不知道为什么你需要共享它),你可以使用HttpContext来获取对你的页面的引用,并从那里获得你的主页:

Public Shared Function DoSomething() As String
    Dim myPage = TryCast(HttpContext.Current.Handler, Page)
    If myPage IsNot Nothing Then
        Dim myMaster As main = TryCast(myPage.Master, main)
        If myMaster IsNot Nothing Then
            myMaster.ltCurrency.Text = "SOME TEXT" 'throws error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.    
            myMaster.CurrencyText = "SOME TEXT" 'this property isn't found at all
        End If
    End If

答案 1 :(得分:0)

第1步:在内容页面中创建一个活动。

Public Event DoSomething(sender as object, myString as String)

第2步:在您的主页上为您刚刚在内容页面中创建的事件添加一个事件处理程序。

Addhandler contentPage.DoSomething, AddressOf ChangeCurrentText

第3步:在处理程序中做任何你想做的事。

Private Sub ChangeCurrentText(sender, text)
ltCurrency.Text = text
End Sub

步骤4:在内容页面中提升事件

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    RaiseEvent DoSomething(ME, "BLAH BLAH")
End Sub