Add <Script … /> tag within <Head … /> tag in ServerControl (ASP.NET)?

吃可爱长大的小学妹 提交于 2019-12-01 07:28:04

问题


I'm gonna use JQuery files in my custom ServerControl , thus I have to add below line within Head tag.

<script type="text/javascript" src="jquery-1.4.3.min.js"></script>

How can I do it in ServerControl with C#


回答1:


You can register custom scripts using the ClientScriptManager.RegisterClientScriptInclude Method during the page load. Alternatively, you can just include the script in your .aspx page. If this is a public server control, the first method is probably more preferable.

EDIT: alternatively you can register scripts in the <head> tag of the page as follows:

HtmlGenericControl jqueryInclude = new HtmlGenericControl("script");
jqueryInclude.Attributes.Add("type", "text/javascript");
jqueryInclude.Attributes.Add("src", "http://<path to jQuery>");
Page.Header.Controls.Add(jqueryInclude);


来源:https://stackoverflow.com/questions/4117712/add-script-tag-within-head-tag-in-servercontrol-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!