ASP.NET that can wrap a JS block and extract it so I can put it back in the header
As a header, basically I have a custom control placed inside the page and then the page is placed inside the master page. I want to extract a javascript block and put it back in the page header. When I try to wrap the code inside any block control (like server-to-server divs) and access divID.InnerText then ASP.NET disconnects with
Cannot get inner content of startup_script because the contents are not literal.
I don't want to extract the JS inside the cs file, this is a terribly ugly approach (all sorts of adaptations and people don't even notice that you have JS written if they didn't drill into your CS file), what can I do?
a source to share
You can save the javascript in a separate file and then add it to the page using Page.RegisterClientScriptBlock ()
Add the javascript you want to the .js file and add the .js file to your project. Modify the properties of the .js file to be an embedded resource.
Then use code like this in your UserControl (perhaps Page_Load) to pull the code out of the file and move it to the page:
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 () puts the script near the top of the page, but apparently not in the header of the page.
(edited bit about title after comment)
a source to share
<%@ 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>
a source to share
Ok, I probably did it horribly, horribly, but I wanted to add a script to the head element in a recent project from a custom control. This script didn't require any data from my server, but I only wanted it on certain pages, so I put the script in a .js file and added it to the head like this:
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 really sure if the code works the way I think it works since the code I wrote for the project is on a different computer, but you get the idea, right?
Edit: After doing a bit of tinkering with your error message, I think this might be the 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 desired result. I haven't tested this, but it works in my head.
a source to share