C# ASP.NET 动态插入代码到头部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1982099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET dynamically insert code into head
提问by Kyle
I'm working inside of a Web User Control (.ascx) that is going to be included in a regular web form (.aspx), but I need to be able to dynamically insert code into the head of the document from the User Control. In my Coldfusion days <cfhtmlhead> would do the trick. Is there an equivalent of this in ASP.NET or a similar hack?
我正在一个 Web 用户控件 (.ascx) 中工作,该控件将包含在常规 Web 表单 (.aspx) 中,但我需要能够从用户控件动态地将代码插入文档的头部. 在我的 Coldfusion 时代,<cfhtmlhead> 可以解决问题。在 ASP.NET 或类似的 hack 中是否有等价物?
采纳答案by Eilon
To add HTML markup you can do the following:
要添加 HTML 标记,您可以执行以下操作:
In your UserControl's code you can access Page.Header, which is itself a control. To that control you can then add new controls:
在您的 UserControl 代码中,您可以访问 Page.Header,它本身就是一个控件。然后,您可以向该控件添加新控件:
HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);
To add script markup you don't need access to the head tag at all since ASP.NET has helper methods on the ClientScriptManagerthat do the work for you:
要添加脚本标记,您根本不需要访问 head 标记,因为 ASP.NET 在ClientScriptManager上有帮助方法为您完成工作:
Here are examples of some code you can also put in your user control's code:
以下是您也可以放入用户控件代码中的一些代码示例:
// Register some inline script:
Page.ClientScript.RegisterClientScriptBlock(GetType(), "myAlertScript", "alert('hello!')", true);
// Register a script reference:
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js");
回答by Gabriel McAdams
this.Page.Header.Controls.Add
By doing this, you are adding controls to the head section. You can add any type of control. If you feel you need to add simple text (or you want to write the tags manually), then look into the LiteralControlclass.
通过这样做,您正在向 head 部分添加控件。您可以添加任何类型的控件。如果您觉得需要添加简单的文本(或者您想手动编写标签),请查看LiteralControl类。
回答by Skeolan
There's some guidance on using C# code to modify the page header here. It should work just fine from any server-side code that executes before the page load completes.
有一个关于使用C#代码来修改页面页眉一些指导这里。它应该可以在页面加载完成之前执行的任何服务器端代码中正常工作。
A simple e.g.
一个简单的例如
HtmlHead head = Page.Header;
HtmlTitle title = new HtmlTitle();
title.Text = "Test Page";
head.Controls.Add(title);
HTMLHead reference is in namespace
HTMLHead 引用位于命名空间中
System.Web.UI.HtmlControls
Override the custom control's Load() method to add the controls or references you need into the page header while the parent .aspx page is being loaded server-side.
在服务器端加载父 .aspx 页面时,重写自定义控件的 Load() 方法以将所需的控件或引用添加到页眉中。
回答by Joshua Dannemann
I have a simple solution for this. Create a runtime memory cache based on the url of the page (as a key) that holds x information about y (be it a file reference, script text, or class that generates JavaScript) and serialize its data to JSON. Newtonsoft is helpful for instances of any class. In fact, you can use it's output to initialize any new instance of a class based upon given input. In a way, that means you may have your instances of any particular class automatically instantiated despite what user control the instance is on. In the end, you create a simple web form to serve as a script reference and as the final endpoint. It pulls the JavaScript (or what've it) and spits out the client side code you need as a script reference inside the head tag.
我有一个简单的解决方案。根据页面的 url(作为键)创建一个运行时内存缓存,其中包含关于 y 的 x 信息(可以是文件引用、脚本文本或生成 JavaScript 的类),并将其数据序列化为 JSON。Newtonsoft 对任何类的实例都有帮助。事实上,您可以使用它的输出根据给定的输入初始化类的任何新实例。在某种程度上,这意味着您可以自动实例化任何特定类的实例,而不管该实例处于何种用户控制状态。最后,您创建一个简单的 Web 表单以用作脚本引用和最终端点。它提取 JavaScript(或其他内容)并吐出您需要的客户端代码作为 head 标记内的脚本引用。
回答by nu everest
I realize that this is an old question, but this is another example.
我意识到这是一个老问题,但这是另一个例子。
Try This:
尝试这个:
Page.Header.Controls.Add(
new LiteralControl(
"<script>alert('Literal Added to <Head>.');</script>"
)
);
If you want to add the script at a particular index of the <head>
you can use
如果您想在<head>
您可以使用的特定索引处添加脚本
AddAt(index, new LiteralControl(...))
where index 0 equals the top of the <head>
AddAt(index, new LiteralControl(...))
其中索引 0 等于顶部 <head>
Also, you need to add runat="server"
in your head tag e.g. <head id="head1" runat="server">
此外,您需要添加runat="server"
您的头部标签,例如<head id="head1" runat="server">