asp.net background to output script style to head tag to save code redundancy _ practical Skills

Source: Internet
Author: User

Recently learning to develop server controls, the other is to register for the control of JS and CSS, such as resource files, or directly register a script-only style. The following problems are encountered:

1, the registered resource file or plain script style in the generated page is not in the head tag (of course this does not affect the page function)

2, a page to use many of the same control, there will be repeated input (the occurrence of redundant code)

The first question, in the final analysis, is not a problem, mainly to see a person like. In the browser to view the page source code, may become a problem, the source code is very untidy, if the content of the problem is more prominent. Originally wanted to find a script, but in the head tag can not find, only to find the other tags. (I don't know if there are any development tools that can differentiate them when viewing the source code to make it easier to find)

The second one is actually a problem, and it's not much to say.

There is a problem should be resolved, in order to facilitate the viewing effect, changed it to the background directly used, the development of server controls are also used, but do not refer to Embedded resource files.

The code is as follows, two methods:

Copy Code code as follows:

Registering a resource file

<summary>
Registering a resource file
</summary>
<param name= "path" > Path </param>
<param name= "Key" > The key to the client resource to search to prevent </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
}
Whether it has been exported
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);//Register resource key
}
}


This method has three parameters, the first path is the resource file path, and the second key is the resource file identity, which is used to prevent duplicate registrations, and the third type, the enumeration type, the style, and the scripting two categories. Method is also simple, by adding your own defined controls for the page header control to achieve the desired effect. Page.ClientScript.IsClientScriptBlockRegistered (this. GetType (), key) is used to detect whether this resource file identity has been registered in the current page instance, Page.ClientScript.RegisterClientScriptBlock (this. GetType (), Key, "", false) this is not small, this effect is to register the resource in the current page instance, which originally meant to register a script, but the script here is empty.

Copy Code code as follows:

Registering a script block (or style block)

<summary>
Registering a 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)
{
Whether it has been exported
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);//Register resource key
}
}


This method has two parameters, and the first script is a scripting block (or a style block), such as <script>******</script> or both <style></style>. The method body is similar to the above and is not spoken here.

  

How to use

This example is used in the Page_Load method.

Copy Code code as follows:

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 (' direct script input ') </script>", "Dfed6");
}

Style file:
Stylesheet1.css
Copy Code code as follows:

Body {
}

div {height:200px; Background-color:blue}


Script file:
Jscript1.js
Copy Code code as follows:

Alert (' This is the script in the JS file ');

Page:
Html
Copy Code code as follows:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.