4

在试图理解为什么客户端代码没有呈现在页面中(由用户控件注入)后,我找到了这个链接,事实证明你必须有一个表单标签才能工作(Page.RegisterClientScriptBlock确实声明了这一点,但我的ClientScriptManager.RegisterClientScriptBlock use 对此没有任何说明)。
我正在使用 Visual Studio 2005。
有谁知道这是否已解决?

编辑
为了澄清,我希望我的控件将 javascript 代码添加到页面的 head 部分,而不必使用

<form runat="server"

我尝试使用以下方法添加它:

HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = "alert('123');";
Page.Header.Controls.Add(x);

但这对我不起作用。

4

4 回答 4

2

据我所知,这个功能在当前版本中是一样的,不过你可以很简单地测试它。

更新

根据评论中的讨论,我能想到的唯一“解决方法”是让您自己手动将脚本插入页面的“head”部分,在 Head 元素上使用 runat="server" 声明.

于 2009-01-26T15:08:50.723 回答
2

Got it!
My mistake was not doing it in the OnPreRender method (I used the Render method).

Now all that is needed is - like Mitchel Sellers wrote, set the header to runat server and than add to it's controls:

 HtmlGenericControl x = new HtmlGenericControl("script");
 x.InnerText = GetScriptSection();
 Page.Header.Controls.Add(x);

Thanks for pointing me to the right direction!

于 2009-01-26T15:46:25.250 回答
0

registerclientscriptblock 的 MSDN 页面在这里说:

客户端脚本在 Page 对象的 <form runat= server> 元素的开始标记之后发出。脚本块在定义呈现输出的对象时发出,因此您必须包含 <script> 元素的两个标记。

如果您不想包含表单,那么您基本上需要构建自己的实现。

于 2009-01-26T15:25:50.283 回答
0

Minor clarification for anyone seeing this:

The form tag must have the runat="server" attribute set, e.g.

<form id="theform" runat="server">

Just placing a regular HTML form tag in the page will not help.

于 2009-02-26T18:12:06.340 回答