As title, basically I have a user control placed inside a page and then the page is placed inside the master page. I want to extract a block of javascript and put it back to the page head. When I try to wrap the code inside any block control (e.g. a div runat server) and access divID.InnerText then ASP.NET bust off with
作为标题,基本上我有一个用户控件放在页面内,然后页面放在母版页内。我想提取一个javascript块并将其放回页面头部。当我尝试将代码包装在任何块控件(例如div runat服务器)中并访问divID.InnerText时,ASP.NET就会破坏
Cannot get inner content of startup_script because the contents are not literal.
I dont want to extract JS inside cs file, thats awfully ugly approach (all sort of escapes and people wont even notice you have JS written unless they drill your CS file), what can I do?
我不想在cs文件中提取JS,这是非常丑陋的方法(所有类型的逃脱,人们甚至不会注意到你写了JS,除非他们钻你的CS文件),我该怎么办?
3 个解决方案
#1
You could store the javascript in a separate file, and then add it to the page using Page.RegisterClientScriptBlock()
您可以将javascript存储在单独的文件中,然后使用Page.RegisterClientScriptBlock()将其添加到页面中
Add the javascript you want to a .js file and add the .js file to your project. Alter the properties of the .js file so that it is an Embedded Resource.
将您想要的javascript添加到.js文件中,并将.js文件添加到项目中。更改.js文件的属性,使其成为嵌入式资源。
Then use code like this somewhere in your UserControl (maybe the Page_Load) to pull the code from the file and drop it into the page:
然后在UserControl中的某个地方使用这样的代码(可能是Page_Load)从文件中提取代码并将其放入页面:
string javaScript = "";
// the javascript is in a separate file which is an 'embedded resource' of the dll
using (StreamReader reader =
new StreamReader((typeof(ThisClass).Assembly.GetManifestResourceStream(typeof(ThisClass), "NameOfJavaScriptFile.js"))))
{
javaScript = String.Format("<script language='javascript' type='text/javascript' >\r\n{0}\r\n</script>", reader.ReadToEnd());
}
Page.RegisterClientScriptBlock("MyScriptBlock", javaScript);
Note that RegisterClientScriptBlock() will put the script near the top of the page, but apparently not in the page header.
请注意,RegisterClientScriptBlock()会将脚本放在页面顶部附近,但显然不在页眉中。
(edited bit about header after comment)
(评论后关于标题的编辑位)
#2
<%@ Page Language="C#"%>
<script runat=server>
protected override void OnLoad(EventArgs e)
{
string scriptText = someID.InnerHtml;
//if you really want it in the header...
//Page.Header.Controls.Add( new LiteralControl(String.Format( "<scr" + "ipt language=\"javascript\">{0}</scri" + "pt>\\n", scriptText )));
//doesnt add to header and requires form runat=server
Page.ClientScript.RegisterStartupScript(this.GetType(), "SomeScript", scriptText, true);
base.OnLoad(e);
}
</script>
<head runat=server></head>
<form runat=server>
<div id="someID" runat=server>alert('hi');</div>
</form>
#3
Okay, I've probably done this in a horrible, horrible way, but I wanted to add a script to the head element in a recent project from a user control. This script didn't require any data from my server, but I only wanted it on specific pages, so I put the script in a .js-file, and added it to the head like this:
好吧,我可能以一种可怕的,可怕的方式做到了这一点,但我想在一个来自用户控件的最近项目中的head元素中添加一个脚本。这个脚本不需要我服务器上的任何数据,但我只想在特定页面上使用它,所以我将脚本放在.js文件中,并将其添加到头部,如下所示:
HtmlGenericControl script = new HtmlGenericControl("script"); // Creates a new script-element
script.Attributes.Add("type","text/javascript");
script.Attributes.Add("src","src/to/js-file.js");
Page.Master.FindControl("head").Controls.Add(script); // Finds the element with ID "head" on the pages master page.
Not entirely sure if the code works as I think it does as the code I wrote for the project is on another machine, but you get the idea, right?
不完全确定代码是否像我认为的那样工作,因为我为项目编写的代码在另一台机器上,但是你明白了,对吧?
Edit: After googling your error message for a bit, I think this might be a solution to your problem:
编辑:在搜索您的错误消息一段时间后,我认为这可能是您的问题的解决方案:
using System.IO;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
yourDivID.RenderControl(h);
String str = sw.GetStringBuilder().ToString();
If you combine the two examples, you should be able to get the result you want. I haven't tested this, but it works in my head.
如果将这两个示例结合起来,您应该能够获得所需的结果。我没有测试过这个,但它在我脑海中起作用。
#1
You could store the javascript in a separate file, and then add it to the page using Page.RegisterClientScriptBlock()
您可以将javascript存储在单独的文件中,然后使用Page.RegisterClientScriptBlock()将其添加到页面中
Add the javascript you want to a .js file and add the .js file to your project. Alter the properties of the .js file so that it is an Embedded Resource.
将您想要的javascript添加到.js文件中,并将.js文件添加到项目中。更改.js文件的属性,使其成为嵌入式资源。
Then use code like this somewhere in your UserControl (maybe the Page_Load) to pull the code from the file and drop it into the page:
然后在UserControl中的某个地方使用这样的代码(可能是Page_Load)从文件中提取代码并将其放入页面:
string javaScript = "";
// the javascript is in a separate file which is an 'embedded resource' of the dll
using (StreamReader reader =
new StreamReader((typeof(ThisClass).Assembly.GetManifestResourceStream(typeof(ThisClass), "NameOfJavaScriptFile.js"))))
{
javaScript = String.Format("<script language='javascript' type='text/javascript' >\r\n{0}\r\n</script>", reader.ReadToEnd());
}
Page.RegisterClientScriptBlock("MyScriptBlock", javaScript);
Note that RegisterClientScriptBlock() will put the script near the top of the page, but apparently not in the page header.
请注意,RegisterClientScriptBlock()会将脚本放在页面顶部附近,但显然不在页眉中。
(edited bit about header after comment)
(评论后关于标题的编辑位)
#2
<%@ Page Language="C#"%>
<script runat=server>
protected override void OnLoad(EventArgs e)
{
string scriptText = someID.InnerHtml;
//if you really want it in the header...
//Page.Header.Controls.Add( new LiteralControl(String.Format( "<scr" + "ipt language=\"javascript\">{0}</scri" + "pt>\\n", scriptText )));
//doesnt add to header and requires form runat=server
Page.ClientScript.RegisterStartupScript(this.GetType(), "SomeScript", scriptText, true);
base.OnLoad(e);
}
</script>
<head runat=server></head>
<form runat=server>
<div id="someID" runat=server>alert('hi');</div>
</form>
#3
Okay, I've probably done this in a horrible, horrible way, but I wanted to add a script to the head element in a recent project from a user control. This script didn't require any data from my server, but I only wanted it on specific pages, so I put the script in a .js-file, and added it to the head like this:
好吧,我可能以一种可怕的,可怕的方式做到了这一点,但我想在一个来自用户控件的最近项目中的head元素中添加一个脚本。这个脚本不需要我服务器上的任何数据,但我只想在特定页面上使用它,所以我将脚本放在.js文件中,并将其添加到头部,如下所示:
HtmlGenericControl script = new HtmlGenericControl("script"); // Creates a new script-element
script.Attributes.Add("type","text/javascript");
script.Attributes.Add("src","src/to/js-file.js");
Page.Master.FindControl("head").Controls.Add(script); // Finds the element with ID "head" on the pages master page.
Not entirely sure if the code works as I think it does as the code I wrote for the project is on another machine, but you get the idea, right?
不完全确定代码是否像我认为的那样工作,因为我为项目编写的代码在另一台机器上,但是你明白了,对吧?
Edit: After googling your error message for a bit, I think this might be a solution to your problem:
编辑:在搜索您的错误消息一段时间后,我认为这可能是您的问题的解决方案:
using System.IO;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
yourDivID.RenderControl(h);
String str = sw.GetStringBuilder().ToString();
If you combine the two examples, you should be able to get the result you want. I haven't tested this, but it works in my head.
如果将这两个示例结合起来,您应该能够获得所需的结果。我没有测试过这个,但它在我脑海中起作用。