从用户控件.ASP.NET添加脚本到页面

时间:2010-04-30 13:25:34

标签: asp.net

如何从用户控件向页眉添加javascript文件?

由于

2 个答案:

答案 0 :(得分:0)

使用Page.RegisterStartupScript("pranay","javascriptFunction()")

把这个东西放在你用户控件的加载方法

检查一下:

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx

答案 1 :(得分:0)

您是否在网站中使用母版页?如果是,则应在母版页中执行所有此类包含。但是你也可能面临javascript文件路径的问题。您可以按以下方式包含该文件:

<script type="text/javascript" language="javascript" src="<%= this.ResolveClientUrl("~/Script/jquery.js") %>"></script>

如果您想从用户控制中执行此操作。请尝试以下方法:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Define the name, type and url of the client script on the page.
        Dim csname As String = "ButtonClickScript"
        Dim csurl As String = "~/script_include.js"
        Dim cstype As Type = Me.GetType()

        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Check to see if the include script is already registered.
        If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then

            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))

        End If

    End Sub