天天看点

Asp.Net之后台载入JS和CSS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Text;

namespace AspNetLoadJsCss.Common
{
    public class PageBase : System.Web.UI.Page
    {       
        public static readonly string SCRIPT_INCLUDE_TEMPLATE = "<script src=\"{0}\" type=\"text/javascript\"></script>\r\n";
        public static readonly string STYLE_INCLUDE_TEMPLATE = "\r\n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>\r\n";
        public static readonly string SCRIPT_CONTENT_TEMPLATE = "<script type=\"text/javascript\">{0}</script>\r\n";

        protected void Page_InitComplete(object sender, EventArgs e)
        {          
            LiteralControl viewportControl = new LiteralControl();
            viewportControl.ID = "viewport";
            viewportControl.Text = "\r\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />";
            this.Header.Controls.AddAt(GetNextControlIndex(this), viewportControl);

            LiteralControl jqueryCssControl = new LiteralControl();
            jqueryCssControl.ID = "/jquery.mobile-1.4.4.min.css";
            jqueryCssControl.Text = String.Format(STYLE_INCLUDE_TEMPLATE, "/css/themes/default/jquery.mobile-1.4.4.min.css");
            this.Header.Controls.AddAt(GetNextControlIndex(this), jqueryCssControl);

            LiteralControl myCssControl = new LiteralControl();
            myCssControl.ID = "/my.css";
            myCssControl.Text = String.Format(STYLE_INCLUDE_TEMPLATE, "/css/my.css");
            this.Header.Controls.AddAt(GetNextControlIndex(this),myCssControl);


            String jsPath = "/js/jquery.js";
            this.ClientScript.RegisterStartupScript(this.GetType(),
                "jquery",
                String.Format(SCRIPT_INCLUDE_TEMPLATE, jsPath),
                false);

            jsPath = "/js/jquery.mobile-1.4.4.      

继续阅读