2

I have a abstract user control that provides common functionality across a few controls, it requires some javascript functions to hook up client side events, what is the best way to inject this javascript.

I did have a look at ScriptManager.RegisterClientScriptBlock but it seems that this is aimed at using UpdatePanels and the like which I am not doing, would this still work correctly if all I want to do is have this javascript located in a single place and rendered from the abstract user control?

2 Answers 2

2

If you are not rendering in an async postback, then use the ClientScriptManager's RegisterClientScriptBlock instead. You can access this from the Page.ClientScript property. If you use the ScriptManager's method, it should work, but it would require that the page have a ScriptManager on it in addition to your control.

Sign up to request clarification or add additional context in comments.

Comments

1

Another option is to include a javascript file in the page header, like this:

LiteralControl jsResource = new LiteralControl();
jsResource.Text = "<script type=\"text/javascript\" src=\"js/mini-template-control.js\"></script>";
Page.Header.Controls.Add(jsResource);

HtmlLink stylesLink = new HtmlLink();
stylesLink.Attributes["rel"] = "stylesheet";
stylesLink.Attributes["type"] = "text/css";
stylesLink.Href = "css/mini-template-control.css";
Page.Header.Controls.Add(stylesLink);

Here is a similar question I found:

Programmatically adding Javascript File to User Control in .net

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.