Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2209
  • Last Modified:

C#, Adding Javascript to document head with registration

Is there any way to access the JavaScript registration information on a page.  I need to include JavaScript in the head of a document (which I can do), but since I have this code running on different pages it needs to check if it's registered first before adding it so there's not multiple includes.  You know - the same way asp checks for registration on every other kind of javascript except head includes.

I thought I could trick it by using the attached snippet, but that doesn't work.  Is there any way to access the registion information built into .NET so I can manually register and check these scripts?
if(!_control.Page.ClientScript.IsClientScriptBlockRegistered(_key)) {
	_control.Page.ClientScript.RegisterClientScriptBlock(_control.GetType(), _key, "var " + Guid.NewGuid().ToString() + "={};", true);
	HtmlGenericControl Include = new HtmlGenericControl("script");
	Include.Attributes.Add("type", "text/javascript");
	Include.Attributes.Add("src", script);
	_control.Page.Header.Controls.Add(Include);
}

Open in new window

0
maiello
Asked:
maiello
1 Solution
 
maielloAuthor Commented:
I was finally able to get it:

 if(!_control.Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), _key)) {
    _control.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), _key, " ", true);
    HtmlGenericControl Include = new HtmlGenericControl("script");
    Include.Attributes.Add("type", "text/javascript");
    Include.Attributes.Add("src", script);
    _control.Page.Header.Controls.Add(Include);
}

Open in new window

0
 
EE_AutoDeleterCommented:
maiello,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now