我正在从内容页调用主页面的共享函数。在这个共享函数中,我希望访问主页面中的控件,但我不知道该怎么做。
主页面为main.master。
主页面为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(main,Masterpage)。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
我还能做什么?
Master
属性转换为你的MasterPage
的实际类型。看一下我的代码,因为我已经这样做了。 - Tim SchmelterIf myPage IsNot Nothing Then Dim master = myPage.Master Dim myMaster = TryCast(master.Master, ERPMaster) While master.Master IsNot Nothing AndAlso myMaster Is Nothing master = master.Master myMaster = TryCast(master, ERPMaster) End While ' ... ' End If
- Tim Schmelter