简体   繁体   中英

Add script to page from user control .ASP.NET

how can i add a javascript file to the page head from a user control?

Thanks

use Page.RegisterStartupScript("pranay","javascriptFunction()")

put this thing in you load method of user control

check this out :

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

Are you using master page in your site? If yes then you should do all such includes in the master page. But then also you might face issue with the path of javascript file. You can include the file in following way:

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

If you want to do it from User Control. Try the following:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM