0

我需要一些关于 jQuery 的帮助。目前我正在构建包含 div 的服务器控件。在那个 div 里面有一个 iFrame。我希望能够使用 jQuery 调整 div 的大小。我通过简单的示例实现了这一点。

正如我所说,我正在构建一个服务器控件,所以我不能在每个页面上手动添加 jQuery。它应该是自动化的,当我将控件拖放到设计器表面时,我不必担心调整 Div 的大小。

目前我正在母版页中注册所有相关的 *.js 文件。我有一个继承母版页的子页面。在那个页面上,我有我的控制权。我无法制作 *.js 文件并注册它,因为我可以拥有多个相同控件的实例。

我在哪里注册我的脚本块?它是如何完成的?

对此的任何澄清将不胜感激。

4

1 回答 1

1

这是服务器控件还是 .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)

无论哪种方式,如果您有更多问题,请告诉我您使用的是哪种类型,并发布您到目前为止添加的相关代码。

于 2013-05-28T17:06:18.160 回答