这是服务器控件还是 .Ascx 服务器控件?如果是服务器控件,它继承自什么类型的控件?如果你从 WebControl 继承,你可以做这样的事情(这是一个简短的例子,所以需要你做一些工作):
Public Class myControl
Inherits WebControl
Private Sub attachWebResources()
Dim styleLink As String = "<link rel='stylesheet' text='text/css' href='{0}' />"
Dim location As String = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "myApp.WebControls.myStyles.css")
Dim styleInclude As New LiteralControl([String].Format(styleLink, location))
DirectCast(Page.Header, HtmlControls.HtmlHead).Controls.Add(styleInclude)
ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "myApp.WebControls.jquery-1.4.1.min.js")
EnsureChildControls()
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
attachWebResources()
MyBase.OnInit(e)
End Sub
End Class
这个例子展示了如何在你的控件中包含一个嵌入的 CSS 和 JS 文件。您需要在 Web 控件库项目中包含 JS 和 CSS 文件。然后,您需要在您的 AssemblyInfo.Vb 文件中为您的 JS 文件添加一个引用,如下所示:
<Assembly: Web.UI.WebResource("myApp.WebControls.jquery-1.4.1.min.js", "text/javascript")>
如果这是一个 ASCX Web 控件,或者任何类型的服务器控件,您可以添加如下代码:
Dim myScript As New StringBuilder
myScript.Append("function helloWorld(){" & vbCrLf)
myScript.Append("alert('hello world')" & vbCrLf)
myScript.Append("}" & vbCrLf)
Page.ClientScript.RegisterStartupScript(Me.GetType(), "myKey", myScript.tostring, True)
无论哪种方式,如果您有更多问题,请告诉我您使用的是哪种类型,并发布您到目前为止添加的相关代码。