1

I have a base page , and all the pages are inherited from it . and different pages would load some external js files individually. Let's say one of these pages AddUser.aspx.

public class BasePage : System.Web.UI.Page {
   ....
}

public partial class AddUser : BasePage {
   ....
}

After AddUser.aspx is rendered. The html content of it shows some js files would be included like below.

<script src="a.js" type="text/javascript"></script>
<script src="b.js" type="text/javascript"></script>
<script src="c.js" type="text/javascript"></script>
<script src="d.js" type="text/javascript"></script>
...

I want to register an external js file before all these js file. Let's say it is named firstrun.js and make sure firstrun.js always firstly run before other js files in all the page.

the desired result like below.

<script src="firstrun.js" type="text/javascript"></script>
<script src="a.js" type="text/javascript"></script>
<script src="b.js" type="text/javascript"></script>
<script src="c.js" type="text/javascript"></script>
<script src="d.js" type="text/javascript"></script>
...

Is there any workaround in the base page ? thanks.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180

2 Answers2

1

Personally I find .NET's method of inserting JS files troublesome (particularly once you get into master pages where the page's relative path changes).

I don't think it's an elegant solution but the best way I've found of doing is to create a string literal.

<asp:Literal runat="server" ID="ScriptsLiteral" />

Then in your CS

this.Scripts.Text = string.Concat("<script src=\"", this.Page.ResolveUrl("~/Scripts/MyScript.js"), "\"</script>");

By doing it this way you can guarantee where on your page the JS file will appear and that the link will be correct for any page in your site.

You can also use a GenericHtmlControl or a UserControl/Server Control to do the same thing.

Liath
  • 9,913
  • 9
  • 51
  • 81
  • If I didn't misunderstood you, Does it mean I need to add `` in all my pages ? Is there any way to add it in the base page ? thanks. – Joe.wang Feb 27 '14 at 10:38
  • I add it to a master page, if you're using a base page rather than a master page can you add it programmatically there? – Liath Feb 27 '14 at 10:39
  • So the problem turn to "How can I add a Literal as the first element of body programmatically ?" , Isn't it ? – Joe.wang Feb 27 '14 at 10:51
  • @Joe.wang there are a number of ways to do it but it's very difficult to advise without seeing your pages (ideally the markup) - perhaps using a content placeholder in your head section? – Liath Feb 27 '14 at 10:54
  • 1
    Thanks your help. +1. I found a way to make it .please help to review it .thanks. – Joe.wang Feb 27 '14 at 12:05
1

Thanks Liath's answer and help.

I found a way to insert the specified js in the header. Please review it .

public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {   
            //this.Page.Header is not ready. it is null. so I move it to PreRender
            base.OnPreInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Literal tempLit = new Literal();
            tempLit.Text = "<script type=\"text/javascript\" src=\"" + this.Page.ResolveUrl("~/js/firstrun.js") + "\"></script>";    
            this.Page.Header.Controls.AddAt(0,tempLit);    
            base.OnPreRender(e);
        }
    }
Joe.wang
  • 11,537
  • 25
  • 103
  • 180