Tutorial :Add script to page from user control .ASP.NET


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



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

put this thing in you load method of user control

check this out :



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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »