Programmatically adding Javascript File to User Control in .net

后端 未结 8 1917
不知归路
不知归路 2020-12-24 08:08

How do you add Javascript file programmatically to the user control?

I want the user control to be a complete package - ie I don\'t want to have to add javascript th

相关标签:
8条回答
  • 2020-12-24 09:01

    You can register client script includes using the ClientScriptManager. Page is accessible through the Control.Page property.

    Page.ClientScript.RegisterClientScriptInclude (
      typeof ( MyControl ), "includeme.js", "js/includeme.js"  );
    

    EDIT: Sorry, for a total "complete package", its possible using scripts as Embedded Resources, and aquire dynamic URL's through the WebResource.axd handler. If this is not considered totally complete, then i guess it could be put in App_LocalResources, but it never gonna be just one file, unless the code and script is inline.

    0 讨论(0)
  • 2020-12-24 09:06

    In my site I Place all needed scripts and styles to placeHolder

    <asp:placeHolder runat="Server" ID="phHead">
        <script src="/header/widget/script.js" type="text/javascript"></script>
        <link href="/header/widget/style.css" rel="stylesheet" type="text/css" />
    </asp:placeHolder>
    

    and

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Header.Controls.Add(phHead)
    End Sub
    
    0 讨论(0)
提交回复
热议问题