1

i use asp.net with VS 2010. I have many sites in my project which all used the same javascript tag. So i must always include the tag when i add a new page. So is there the posssiblilty to include the javascripttag always automatically? A possible problem would to forget the tag when i add a new page.

With Javascript tag i mean:

<script type="text/javascript" src="jsSource.js"></script>
2
  • 4
    Are you already using MasterPages? Commented Aug 1, 2013 at 8:36
  • No but is there a possiblity without MasterPages? Commented Aug 1, 2013 at 8:37

2 Answers 2

3

Including the javascript files in a master page and always use it is a possible solution. If you're not using master pages, then create a base class inheriting from Page class and in the OnLoad event register the script with:

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

4 Comments

A third alternative might be to use a module ...?
Yes but if a forget to inheriting the base class? is there a opportunity to must inherit a specific class to all class?
You can specify a base class for all pages in web.config. <pages pageBaseType="MyWeb.UI.MyPageBase" /> And don't forget to breath, or you'll die ;-)
@Oscar: how does it behave with codebehind files? If your codebehind inherits System.Web.UI.Page, does this setting works? Does it add a new level of inheritance (actual class -> base class from the web.config -> system.web.ui.page) ?
0

(diclaimer: I didn't test this code... it's a guess).

You may use a control adapter to hook the page rendering, in order to inject a script reference to your javascript file.

Look at this page to show the source code of a working solution. The advantage of this solution, is that it use an HttpModule to register the script. Thus, you only have to modify the web.config of each projects, and not change the base class of all pages.

Adapted to your situation, this should looks like:

public class ScriptManagerAddModule : IHttpModule
{
    public void IHttpModule.Dispose() { }

    // This is where you can indicate what events in the request processing lifecycle you want to intercept
    public void IHttpModule.Init(System.Web.HttpApplication context)
    {
       context.PreRequestHandlerExecute += new EventHandler(HttpApplication_PreRequestHandlerExecute);
    }

    void HttpApplication_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication httpApplication = sender as HttpApplication;

        if (httpApplication != null)
        {
            Page page = httpApplication.Context.CurrentHandler as Page;
            if (page != null)
            {
                // When standard ASP.NET Pages are being used to handle the request then intercept the
                // PreInit phase of the page's lifecycle since this is where we should dynamically create controls
                page.PreInit += new EventHandler(Page_PreInit);
            }
        }
    }

    void Page_PreInit(object sender, EventArgs e)
    {
        Page page = sender as Page;
        if (page != null)
        {
            // ScriptManagers must be in forms -- look for forms
            foreach (Control control in page.Controls)
            {
                HtmlForm htmlForm = control as HtmlForm;
                if (htmlForm != null)
                {
                    // Look for an existing ScriptManager or a ScriptManagerProxy
                    bool foundScriptManager = false;
                    foreach (Control htmlFormChild in htmlForm.Controls)
                    {
                        if (htmlFormChild is ScriptManager || htmlFormChild is ScriptManagerProxy)
                        {
                            foundScriptManager = true;
                            break;
                        }
                    }

                    // If we didn't find a script manager or a script manager proxy then add one
                    if (!foundScriptManager)
                    {
                        htmlForm.Controls.Add(new ScriptManager());
                    }
                }
            }

            // Addition to the original code
            ScriptManager.GetCurrent().RegisterClientScriptInclude(page, typeof(ScriptManagerAddModule), "myjavascript", "jsSource.js");
            // End of addition to the original code
        }
    }

This code will both ensure the presence of a ScriptManager control, and register a custom javascript file (see my addition at the bottom of the code).

Then, in all web.config files, register the module:

<httpModules>
    <add name="ScriptManagerAddModule" type="YourLibrary.ScriptManagerAddModule"/>
</httpModules>

Please note that you have to put the module code in a class library, referenced by all other projects (or at least having the dll in all bin folders, or GAC, or any location that .net use to look at dll).

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.