How do you add Javascript file programmatically to the user control?
如何以编程方式将Javascript文件添加到用户控件?
I want the user control to be a complete package - ie I don't want to have to add javascript that's related to the user control on the page where it's used, when I can do it inside the control itself.
我希望用户控件是一个完整的包 - 即我不想在页面上添加与用户控件相关的javascript,当我可以在控件本身内完成时。
Since there is no Page object in the user control, how would you do it?
由于用户控件中没有Page对象,你会怎么做?
8 个解决方案
#1
26
In the Page_Load method of control.ascx.cs file:
在control.ascx.cs文件的Page_Load方法中:
LiteralControl jsResource = new LiteralControl();
jsResource.Text = "<script type=\"text/javascript\" src=\"js/mini-template-control.js\"></script>";
Page.Header.Controls.Add(jsResource);
HtmlLink stylesLink = new HtmlLink();
stylesLink.Attributes["rel"] = "stylesheet";
stylesLink.Attributes["type"] = "text/css";
stylesLink.Href = "css/mini-template-control.css";
Page.Header.Controls.Add(stylesLink);
This will load css and Javascript into the head tag of the main page, just make sure that the head has runat="server".
这会将css和Javascript加载到主页面的head标签中,只需确保头部有runat =“server”即可。
#2
8
You can register client script includes using the ClientScriptManager. Page is accessible through the Control.Page property.
您可以使用ClientScriptManager注册客户端脚本。可以通过Control.Page属性访问页面。
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.
编辑:对不起,对于一个完整的“完整包”,可以使用脚本作为嵌入式资源,并通过WebResource.axd处理程序获取动态URL。如果这不被认为是完全完整的,那么我猜它可以放在App_LocalResources中,但它永远不会只是一个文件,除非代码和脚本是内联的。
#3
6
The same as gnomixa's response, only a bit cleaner:
与gnomixa的回复相同,只是更清洁一点:
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "jscript/formfunctions.js";
Page.Header.Controls.Add(js);
from http://www.aspdotnetfaq.com/Faq/How-to-Programmatically-add-JavaScript-File-to-Asp-Net-page.aspx
来自http://www.aspdotnetfaq.com/Faq/How-to-Programmatically-add-JavaScript-File-to-Asp-Net-page.aspx
#4
3
In my site I Place all needed scripts and styles to placeHolder
在我的网站中,我将所有需要的脚本和样式放置到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
#5
2
Good question!
好问题!
A UserControl should be a single package without any dependency on a JavaScript or a CSS file. You will need to make the JS and CSS files as embedded resources. Right click properties and set build action to embedded resources. Then you need to inject the JavaScript and CSS files as WebResources.
UserControl应该是单个包,不依赖于JavaScript或CSS文件。您需要将JS和CSS文件作为嵌入式资源。右键单击属性并将构建操作设置为嵌入资源。然后,您需要将JavaScript和CSS文件注入WebResources。
Here is one article that I wrote yesterday which talks about the same scenario:
这是我昨天写的一篇文章,讲的是同一个场景:
http://highoncoding.com/Articles/502_Creating_RadioButton_Validation_Using_Custom_Validator.aspx
http://highoncoding.com/Articles/502_Creating_RadioButton_Validation_Using_Custom_Validator.aspx
#6
0
I generally do it when rendering the HTML for the control and depending on whether it's a library injection or an instance injection I use the Items collection to specify whether or not I have already produced the code for a control of this type during the current request using a unique identifier.
我通常在为控件呈现HTML时执行此操作,并且根据它是库注入还是实例注入,我使用Items集合来指定在当前请求期间是否已经为此类型的控件生成了代码唯一标识符。
Something to the effect of:
有效的东西:
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
//Check if the Object Script has already been rendered during this request.
if (!Context.Items.Contains("xxx"))
{
//Specify that the Object Script has been rendered during this request.
Context.Items.Add("xxx", string.Empty);
//Write the script to the page via the control
writer.Write([libraryinjection]);
}
//Write the script that instantiates a new instance for this control
writer.Write([instanceinjection]);
}
#7
0
If you have the text of the actual javascript in your .CS file, you can call Page.ClientScript.RegisterClientScriptBlock.
如果您的.CS文件中包含实际javascript的文本,则可以调用Page.ClientScript.RegisterClientScriptBlock。
The following assumes that "GetScript()" returns the actual javascript you want added to the rendered control.
以下假设“GetScript()”返回您要添加到渲染控件的实际javascript。
Page.ClientScript.RegisterClientScriptBlock(GetType(), "controlScriptName", GetScript());
#8
0
I found this to be a more elegant way to fix-link to your javascript.
In your .ascx, link to your script files the following way:
我发现这是一种更优雅的方式来修复链接到您的JavaScript。在.ascx中,通过以下方式链接到脚本文件:
<script src='<%= ResolveClientUrl("~/Scripts/jquery-1.7.1.min.js") %>' type="text/javascript"></script>
<script src='<%= ResolveClientUrl("~/Scripts/jquery.maskedinput-1.3.min.js") %>' type="text/javascript"></script>
That way, no matter which subfolder/page you add your user control to, the link to your scripts will always be correctly resolved.
这样,无论您将用户控件添加到哪个子文件夹/页面,都将始终正确解析脚本链接。
#1
26
In the Page_Load method of control.ascx.cs file:
在control.ascx.cs文件的Page_Load方法中:
LiteralControl jsResource = new LiteralControl();
jsResource.Text = "<script type=\"text/javascript\" src=\"js/mini-template-control.js\"></script>";
Page.Header.Controls.Add(jsResource);
HtmlLink stylesLink = new HtmlLink();
stylesLink.Attributes["rel"] = "stylesheet";
stylesLink.Attributes["type"] = "text/css";
stylesLink.Href = "css/mini-template-control.css";
Page.Header.Controls.Add(stylesLink);
This will load css and Javascript into the head tag of the main page, just make sure that the head has runat="server".
这会将css和Javascript加载到主页面的head标签中,只需确保头部有runat =“server”即可。
#2
8
You can register client script includes using the ClientScriptManager. Page is accessible through the Control.Page property.
您可以使用ClientScriptManager注册客户端脚本。可以通过Control.Page属性访问页面。
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.
编辑:对不起,对于一个完整的“完整包”,可以使用脚本作为嵌入式资源,并通过WebResource.axd处理程序获取动态URL。如果这不被认为是完全完整的,那么我猜它可以放在App_LocalResources中,但它永远不会只是一个文件,除非代码和脚本是内联的。
#3
6
The same as gnomixa's response, only a bit cleaner:
与gnomixa的回复相同,只是更清洁一点:
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "jscript/formfunctions.js";
Page.Header.Controls.Add(js);
from http://www.aspdotnetfaq.com/Faq/How-to-Programmatically-add-JavaScript-File-to-Asp-Net-page.aspx
来自http://www.aspdotnetfaq.com/Faq/How-to-Programmatically-add-JavaScript-File-to-Asp-Net-page.aspx
#4
3
In my site I Place all needed scripts and styles to placeHolder
在我的网站中,我将所有需要的脚本和样式放置到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
#5
2
Good question!
好问题!
A UserControl should be a single package without any dependency on a JavaScript or a CSS file. You will need to make the JS and CSS files as embedded resources. Right click properties and set build action to embedded resources. Then you need to inject the JavaScript and CSS files as WebResources.
UserControl应该是单个包,不依赖于JavaScript或CSS文件。您需要将JS和CSS文件作为嵌入式资源。右键单击属性并将构建操作设置为嵌入资源。然后,您需要将JavaScript和CSS文件注入WebResources。
Here is one article that I wrote yesterday which talks about the same scenario:
这是我昨天写的一篇文章,讲的是同一个场景:
http://highoncoding.com/Articles/502_Creating_RadioButton_Validation_Using_Custom_Validator.aspx
http://highoncoding.com/Articles/502_Creating_RadioButton_Validation_Using_Custom_Validator.aspx
#6
0
I generally do it when rendering the HTML for the control and depending on whether it's a library injection or an instance injection I use the Items collection to specify whether or not I have already produced the code for a control of this type during the current request using a unique identifier.
我通常在为控件呈现HTML时执行此操作,并且根据它是库注入还是实例注入,我使用Items集合来指定在当前请求期间是否已经为此类型的控件生成了代码唯一标识符。
Something to the effect of:
有效的东西:
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
//Check if the Object Script has already been rendered during this request.
if (!Context.Items.Contains("xxx"))
{
//Specify that the Object Script has been rendered during this request.
Context.Items.Add("xxx", string.Empty);
//Write the script to the page via the control
writer.Write([libraryinjection]);
}
//Write the script that instantiates a new instance for this control
writer.Write([instanceinjection]);
}
#7
0
If you have the text of the actual javascript in your .CS file, you can call Page.ClientScript.RegisterClientScriptBlock.
如果您的.CS文件中包含实际javascript的文本,则可以调用Page.ClientScript.RegisterClientScriptBlock。
The following assumes that "GetScript()" returns the actual javascript you want added to the rendered control.
以下假设“GetScript()”返回您要添加到渲染控件的实际javascript。
Page.ClientScript.RegisterClientScriptBlock(GetType(), "controlScriptName", GetScript());
#8
0
I found this to be a more elegant way to fix-link to your javascript.
In your .ascx, link to your script files the following way:
我发现这是一种更优雅的方式来修复链接到您的JavaScript。在.ascx中,通过以下方式链接到脚本文件:
<script src='<%= ResolveClientUrl("~/Scripts/jquery-1.7.1.min.js") %>' type="text/javascript"></script>
<script src='<%= ResolveClientUrl("~/Scripts/jquery.maskedinput-1.3.min.js") %>' type="text/javascript"></script>
That way, no matter which subfolder/page you add your user control to, the link to your scripts will always be correctly resolved.
这样,无论您将用户控件添加到哪个子文件夹/页面,都将始终正确解析脚本链接。