我可以在没有runat= " server "的情况下,从用户控件中修改。net中的部分吗?

时间:2022-10-19 23:43:23

I have a page that does not have runat="server" set in the <head/> section. I do not have access to modify any of the code in the page.

在部分,我有一个没有runat="server"的页面。我没有权限修改页面中的任何代码。

This page contains a user control which I do have access to. Can I add a <meta/> tag to the head section of the page from the user control? It needs to be server-side so a javascript solution won't work.

这个页面包含一个我可以访问的用户控件。我可以在用户控件的页眉部分添加 标签吗?它需要是服务器端,所以javascript解决方案不会起作用。

2 个解决方案

#1


2  

One option is to create a Response Filter, and then modify the output before it's sent to the user.

一个选项是创建一个响应过滤器,然后在输出发送给用户之前修改它。

http://www.4guysfromrolla.com/articles/120308-1.aspx

http://www.4guysfromrolla.com/articles/120308 - 1. aspx

#2


2  

You can parse the text in

您可以解析文本。

(this.Page.Controls[0] as LiteralControl).Text

to see where the string <head> starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server".

要查看字符串从何处开始,并在其中插入所需的任何文本,从而将您自己的代码插入到页眉中,而不使用runat="server"标记。

Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head> element would have been marked as runat="server" so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.

但是请注意,这是一种非常简单的方法,可以在最不应该的地方获取代码(否则元素将被标记为runat="server",以便您能够正常访问它)。如果在以后的日期,head元素被更改为ASP,这也会中断。网络控制。它可能不会与母版页一起工作,您将不得不沿着控制树向上走,寻找最上面的文字元素。

#1


2  

One option is to create a Response Filter, and then modify the output before it's sent to the user.

一个选项是创建一个响应过滤器,然后在输出发送给用户之前修改它。

http://www.4guysfromrolla.com/articles/120308-1.aspx

http://www.4guysfromrolla.com/articles/120308 - 1. aspx

#2


2  

You can parse the text in

您可以解析文本。

(this.Page.Controls[0] as LiteralControl).Text

to see where the string <head> starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server".

要查看字符串从何处开始,并在其中插入所需的任何文本,从而将您自己的代码插入到页眉中,而不使用runat="server"标记。

Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head> element would have been marked as runat="server" so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.

但是请注意,这是一种非常简单的方法,可以在最不应该的地方获取代码(否则元素将被标记为runat="server",以便您能够正常访问它)。如果在以后的日期,head元素被更改为ASP,这也会中断。网络控制。它可能不会与母版页一起工作,您将不得不沿着控制树向上走,寻找最上面的文字元素。