Author volnet (can be called my big V)
There are some basic introductions on embedded resources in msdn: "embedded resources "()
The content we will introduce here will make these resources more concise.
Usually we only need to complete the following steps when using embedded resources in ASP. net2.0:
1. Add a resource file, such:
2. Change the compilation method of the resource file to "embedded resource", for example:
3. Add assembly information (assemblyinfo. CS or above the namespace of a specific class), such:
[Assembly: system. Web. UI. webresource ("intelligencetextbox. JScript. intelligencetextbox. js", "application/X-javasRepository ")] [Assembly: system. Web. UI. webresource (" intelligencetextbox.css.intelligencetextboxstylesheet.css "," text/CSS ")]
4. register the resource file to the page file (in the protected override void onPrerender (system. eventargs E), such:
Page. clientscript. registerclientscriptresource (this. GetType (), "intelligencetextbox. JScript. intelligencetextbox. js ");
After this step is completed, the script file will be output to the front-end HTML at prerender. For example:
<SCRIPT src = "/webresource. axd? D = XBIPl09lmgYKinSg7vem6zAjPh9zda0B5YvbMz9cdk-Dtoq3pnz_VUoa1-xOFpiq0 & amp; T = 633419848307656250 "type =" text/javasScripts "> </SCRIPT>. However, we noticed that when registerclientscriptresource is generated, it will be treated as application/X-javas.Therefore, only type = "text/javasAnd does not comply with the input rules of other types of resources. For this reason, I constructed the following class and only finished the output of the text/CSS resource of the plain ype type, but it is easy to expand to support various formats, expansion requires you to do very little. The method for introducing resources is very simple. Idea: adding resources to the page is nothing more than, as shown above. The only solution is that the specific formats of different types are different, such as: CSS: <link href = "{0}" rel = "stylesheet" type = "text/CSS"/> javasUsage: <SCRIPT src = "{0}" type = "text/javasPipeline "> </SCRIPT> and the expected representation is shown in the method shown at above, another problem that needs to be solved is that multiple resources on one page do not need to be registered repeatedly. Whether to add duplicate resources should be left to the user to solve the problem by providingIsresourceregisteredThe method is used to determine the user. [The following code can be compiled by the. net2.0 compiler only after being modified. Otherwise, the. net3.0 or later compiler can be used by default. Sorry!] Call Code (example): protected override void onPrerender (system. eventargs e) {base. OnPrerender (E); If (page! = NULL) {clientscripthelper cs = new clientscripthelper (this. Page); string cssname = "intelligencetextbox.css.intelligencetextboxstylesheet.css"; if (! CS. isresourceregistered (cssname) Cs. registerresource <intelligencetextbox> (this, clientscripthelper. invalid ype. textcss, cssname); // page. clientscript. registerclientscriptresource (this. getType (), "intelligencetextbox. JScript. intelligencetextbox. JS "); string jsname =" intelligencetextbox. JScript. intelligencetextbox. JS "; if (! CS. isresourceregistered (jsname) Cs. registerresource <intelligencetextbox> (this, clientscripthelper. invalid ype. textjavascript, jsname) ;}} source code: using system. web. ui; using system. web. UI. webcontrols; using system. collections; namespace intelligencetextbox {internal class clientscripthelper {private idictionary resourceactuals; // <summary> // get the parent container (PAGE) reference. /// </Summary> /// <Param name = "Container"> </param> Public clientscripthelper (page container) {resourceactuals = container. items;} // <summary> // reigister the resource to the page. /// </Summary> /// <typeparam name = "T"> the type of resource. </typeparam> // <Param name = "sender"> the resource. </param> /// <Param name = "cmdype"> the specified ype of the resource. </param> /// <Param name = "resourcename"> The Name Of Resource. </param> Public void registerresource <t> (T sender, cmdype, string resourcename) where T: Control {resourceinfo = resourcehtmltemplatefactory (cmdype, sender. page); If (sender! = NULL) {literal resourceliteral = new literal (); resourceliteral. TEXT = string. format (resourceinfo. htmltemplate, sender. page. clientscript. getwebresourceurl (typeof (t), resourcename); resourceinfo. where. controls. add (resourceliteral); If (! Resourceactuals. contains (resourcename) resourceactuals. add (resourcename, resourceliteral) ;}/// <summary> // make sure is the resource has been registered in the current page. /// </Summary> /// <Param name = "resourcename"> the name of the resource. </param> // <returns> </returns> Public bool isresourceregistered (string resourcename) {return resourceactuals. contains (resourcename);} // <summary> // the factory to create the right resourceinfo for render. /// </Summary> /// <Param name = "cmdype"> specify ype of the resource. </param> /// <Param name = "Container"> the page will contain the resource </param> /// <returns> </returns> Private Static resourceinfo resourcehtmltemplatefactory (metatype, page container) {resourceinfo resource = new resourceinfo () {htmltemplate = string. empty, where = container. header}; If (jsonype = jsonype. textcss) {resource. htmltemplate = "\ n <link href =" {0} "rel =" stylesheet "type =" text/CSS "/>"; resource. where = container. header;} else if (cmdype = cmdype. textjavascript) {resource. htmltemplate = "\ n <SCRIPT src =" {0} "type =" text/javasUsage "> </SCRIPT>"; resource. where = container. header;} // todo: add the other resourceype resourceinfo instance. return resource;} // <summary> // The infomation of resource depend on the specified ype. /// </Summary> internal class resourceinfo {// <summary> // The HTML syntax of the resource. /// e.g. /// text/CSS: \ n <link href = "{0}" rel = "stylesheet" type = "text/CSS"/>/// </Summary> Public String htmltemplate {Get; set;} // <summary> // the place to render the HTML syntax. /// </Summary> public system. web. UI. htmlcontrols. htmlcontrol where {Get; Set ;}/// <summary> // your ype /// </Summary> Public Enum your ype {textcss, textjavascript }}}# C # column