Solved

C#, Adding Javascript to document head with registration

Posted on 2008-06-26
2
2,195 Views
Last Modified: 2008-08-21
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
Comment
Question by:maiello
2 Comments
 

Author Comment

by:maiello
ID: 21883099
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
 

Accepted Solution

by:
EE_AutoDeleter earned 0 total points
ID: 22286487
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How would you add MULTITHREADING to the attached C# code? 4 51
Connection String 16 43
ASP.NET 5 Templates 2 65
How can I get this SlideToggle to behave? 6 15
This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now