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

Source: Internet
Author: User

Recently, I have been studying and developing server controls. Others have to register resource files such as js and css for controls, or directly register pure script styles. The following problems are encountered:

1. The registered resource file or script-only style is not included in the head label in the generated page (of course, this does not affect the page function)

2. When multiple identical controls are used on a page, repeated input (redundant code) appears)

In the end, the first question is not a problem, mainly because I like it. When you view the page source code in a browser, it may become a problem. The source code is not neat, and the problem becomes more prominent if there are too many contents. I was looking for a script, but I couldn't find it in the head label. I can only find it in other labels. (I don't know if there are any development tools that can be distinguished when viewing the source code to facilitate searching)

The second one is actually a problem, and I will not talk about it much.

If there is a problem, it should be solved. To make it easier to see the effect, it is changed to the background for direct use, and also used when developing server controls, but it does not need to reference embedded resource files.

The Code is as follows:

Copy codeThe Code is as follows:
Register resource files

/// <Summary>
/// Register the resource file
/// </Summary>
/// <Param name = "path"> path </param>
/// <Param name = "key"> key of the client resource to be searched 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;
}
// Output or not
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 the Resource key
}
}

This method has three parameters: the first path is the resource file path; the second key is the resource file ID to prevent repeated registration; the third type, enumeration type, style and script. The method is also very simple. You can add custom controls for the page Header control to achieve the desired effect. Page. clientScript. isClientScriptBlockRegistered (this. getType (), key) is used to check whether the resource file ID has been registered in the current Page instance, Page. clientScript. registerClientScriptBlock (this. getType (), key, "", false) is indispensable. This function is to register the resource in the current page instance. The intention is to register a script, however, the script here is empty.

Copy codeThe Code is as follows:
Register the script block (or style block)

/// <Summary>
/// Register the 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)
{
// Output or not
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 the Resource key
}
}

This method has two parameters. The first script is a script block (or style block ), such as <script> ******* </script> or <style> </style>. The method body is similar to the above, so I will not talk about it here.

  

How to Use

This example is used in the Page_Load method.

Copy codeThe Code is 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 ('input directly using a script ') </script>", "dfed6 ");
}

Style file:
StyleSheet1.css
Copy codeThe Code is as follows:

Body {
}

Div {height: 200px; background-color: Blue}

Script file:
JScript1.js
Copy codeThe Code is as follows:
Alert ('this is the script in the js file ');

Page:
Html
Copy codeThe Code is as follows:


<! 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 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.