如何简化这一点,课堂上的2个方法是相同的 头等。
Public Class Gui
Inherits System.Web.UI.Page
Public Sub CreateStyleLink(ByVal StyleArray() As String)
Dim StrStyleLink As String
For Each StyleName In StyleArray
StrStyleLink = [Global].ArrStyles(StyleName)
Dim ObjScript As New HtmlGenericControl("link")
ObjScript.Attributes.Add("rel", "Stylesheet")
ObjScript.Attributes.Add("type", "text/css")
ObjScript.Attributes.Add("href", StrStyleLink)
Me.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Public Sub CreateScriptLink(ByVal ScriptArray() As String)
Dim ScriptLink As String
For Each ScriptName In ScriptArray
ScriptLink = [Global].ArrScripts(ScriptName)
Dim ObjScript As New HtmlGenericControl("script")
ObjScript.Attributes.Add("type", "text/javascript")
ObjScript.Attributes.Add("src", ScriptLink)
Me.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Public Sub SetSelectedMenu(ByVal MenuName As String, Optional ByRef ObjPage As Page = Nothing)
Dim ObjMenu As New HtmlGenericControl
If ObjPage Is Nothing Then
ObjMenu = MyBase.Page.FindControl(MenuName)
Else
ObjMenu = ObjPage.FindControl(MenuName)
End If
ObjMenu.Attributes.Add("class", "selected")
End Sub
Public Sub ExecuteScript(ByVal ScriptName As String, Optional ByVal ScriptParam As String = "")
Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Me.Page)
If Not requestSM Is Nothing And requestSM.IsInAsyncPostBack Then
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
Else
Me.Page.ClientScript.RegisterStartupScript(Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
End If
End Sub
End Class
二等。
Public Class GuiControl
Inherits System.Web.UI.UserControl
Public Sub CreateStyleLink(ByVal StyleArray() As String)
Dim StrStyleLink As String
For Each StyleName In StyleArray
StrStyleLink = [Global].ArrStyles(StyleName)
Dim ObjScript As New HtmlGenericControl("link")
ObjScript.Attributes.Add("rel", "Stylesheet")
ObjScript.Attributes.Add("type", "text/css")
ObjScript.Attributes.Add("href", StrStyleLink)
Me.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Public Sub CreateScriptLink(ByVal ScriptArray() As String)
Dim ScriptLink As String
For Each ScriptName In ScriptArray
ScriptLink = [Global].ArrScripts(ScriptName)
Dim ObjScript As New HtmlGenericControl("script")
ObjScript.Attributes.Add("type", "text/javascript")
ObjScript.Attributes.Add("src", ScriptLink)
Me.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Public Sub ExecuteScript(ByVal ScriptName As String, Optional ByVal ScriptParam As String = "")
Dim requestSM As ScriptManager = ScriptManager.GetCurrent(Me.Page)
If Not requestSM Is Nothing And requestSM.IsInAsyncPostBack Then
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
Else
Me.Page.ClientScript.RegisterStartupScript(Me.GetType, ScriptName, ScriptName + "('" + ScriptParam + "');", True)
End If
End Sub
End Class
答案 0 :(得分:1)
创建一个定义方法的基础(可能是抽象)类,inherit from that。
或者您可以使用extension method。
答案 1 :(得分:0)
我会将它们创建为实用程序类的简单静态成员,或者如果您愿意,可以将静态成员扩展为UserControl
的扩展方法
这是一个实用程序类的快速示例,未经过测试,VB不是我最常用的语言,所以请原谅任何次要的语法疏忽。
Class NotInheritable UserControlHelper
Public Sub CreateStyleLink(ByVal control as UserControl, _
ByVal StyleArray() As String)
Dim StrStyleLink As String
For Each StyleName In StyleArray
StrStyleLink = [Global].ArrStyles(StyleName)
Dim ObjScript As New HtmlGenericControl("link")
ObjScript.Attributes.Add("rel", "Stylesheet")
ObjScript.Attributes.Add("type", "text/css")
ObjScript.Attributes.Add("href", StrStyleLink)
control.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Public Sub CreateScriptLink(ByVal control as UserControl, _
ByVal ScriptArray() As String)
Dim ScriptLink As String
For Each ScriptName In ScriptArray
ScriptLink = [Global].ArrScripts(ScriptName)
Dim ObjScript As New HtmlGenericControl("script")
ObjScript.Attributes.Add("type", "text/javascript")
ObjScript.Attributes.Add("src", ScriptLink)
control.Page.Header.Controls.Add(ObjScript)
Next
End Sub
Private Sub New()
End Sub
End Class
然后可以从具有UserControl的任何位置使用它作为第一个参数传入。我不确定是否可以在VB.NET中定义扩展方法,我支持2010,但我可能错了。
答案 2 :(得分:0)
创建基类只是为了继承常用方法通常不是要走的路。继承旨在创建更具体的对象类型,而不仅仅是为了共享。在这种情况下,一个是Page,另一个是UserControl,可能没有定义层次关系。
因此,实用程序或帮助程序类可能是要走的路,基本上就像Chris所描述的那样。我要做的一个补充是将两个方法共享,以便不需要使用实例。
通过使用普通的实例方法,您必须沿着这条路线前进:
Dim help As New UserControlHelper
help.CreateScriptLink(Me)
与Shared方法相反,可以像:
一样完成UserControlHelper.CreateScriptLink(Me)
我不记得Page是否继承自UserControl(反之亦然),所以你可能需要找到一个更高的类来传递给方法,但这个想法基本相同。