The Asp.net background outputs the script style to the head tag to save code redundancy

  • 2020-05-26 08:14:48
  • OfStack

While you've recently been learning to develop server controls, you may want to register resource files such as js and css for your controls, or simply register plain script styles. Among them, there are the following problems:

1. Neither the registered resource file nor the pure script style is included in the head tag in the generated page (of course, this does not affect the page function).

2. When multiple 1-like controls are used on one page, repeated input (redundant code) will appear.

The first question is not a problem in the end, it is mainly based on personal preference. This may be a problem when viewing the source code of a page in a browser. The source code is very untidy, especially if there is a lot of content. I was looking for the script, but I couldn't find it in the head tag, so I had to look in the other tags. (I wonder if there are any development tools that can separate them when looking at the source code for easy lookups.)

The second is a real problem, and I won't say much more about it.

The problem should be solved, in order to facilitate the effect, changed it to the background direct use, development server control is also used, but do not reference the embedded resource file.

The code is as follows:


 Register resource file  
/// <summary>
        ///  Register resource file 
        /// </summary>
        /// <param name="path"> The path </param>
        /// <param name="key"> The key to the client resource to search against </param>
        /// <param name="type"> Resource file type </param>
        public void RegisterResource(string path, string key, ResType type)
        {
            string resStr = string.Empty;
            switch (type)
            {
                case ResType.Js:
                    resStr = string.Format("<script type=\"text/javascript\" language=\"javascript\" src=\"{0}\"></script>", path);
                    break;
                case ResType.Css:
                    resStr = string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", path);
                    break;
            }
            // Has it been output? 
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), key))
            {
                if (Page.Header != null)
                {
                    LiteralControl link = new LiteralControl();
                    link.Text = "\r\n" + resStr;
                    Page.Header.Controls.Add(link);
                }
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, "", false);// Registered resources key
            }
        }

This method has three parameters. The first path is the resource file path. The second key is a resource file identifier to prevent duplicate registrations; The third type, enumerated types, styles, and scripts. It is also simple to achieve the desired effect by adding a custom control to the page Header control. Page.ClientScript.IsClientScriptBlockRegistered (this.GetType (), key) is used to detect whether the resource file identifier has been registered in the current page instance. Page.ClientScript.RegisterClientScriptBlock (this.GetType (), key, "", false) is required.


 Register script block ( Or style block ) 
/// <summary>
        ///  Register script block ( Or style block )
        /// </summary>
        /// <param name="script"></param>
        /// <param name="key"></param>
        /// <param name="type"></param>
        public void RegisterScript(string script, string key)
        {
            // Has it been output? 
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), key))
            {
                if (Page.Header != null)
                {
                    LiteralControl link = new LiteralControl();
                    link.Text = "\r\n" + script;
                    Page.Header.Controls.Add(link);
                }
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, "", false);// Registered resources key
            }
        }

This method takes two arguments, with the first script being the script block (or style block), such as < script > ****** < /script > Or all < style > < /style > And so on. The body of the method is similar to the one above, so I won't talk about it here.

How to use

This example is used in the Page_Load method


  protected void Page_Load(object sender, EventArgs e)
        {
this.RegisterResource("css/StyleSheet1.css", "dfed", ResType.Css);
                this.RegisterResource("Scripts/JScript1.js", "dfed4", ResType.Js);
                this.RegisterScript("<script>alert(' Use directly script Script input ')</script>", "dfed6");
        }

Style file:
StyleSheet1.css

body {
}
div { height:200px; background-color:Blue}

Script file:
JScript1.js

alert(' This is a js The script in the file ');

Page:
html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>


Related articles: