Webresource-asp.net custom controls referencing external resource methods

Source: Internet
Author: User

Rom:http://www.lmwlove.com/ac/ID879

when developing custom controls in ASP, if we are going to use files such as pictures, external css,js, and so on, the best way to do this is to embed these files as a resource for custom controls, and package them as DLL files as they are published. How do we set these files as embedded resources, and how do we reference them in custom controls? Below we take a CSS and JS file as an example to do a demonstration.

One, the first of course is to create these two files. We created two files within the custom control's project, Jscript.js and Stylessheet.css files, respectively. To make them an embedded resource, the most important thing is to right-click on the file directory, select Properties, select "Embedded Resources" in the options of the build operation, as follows:

set the properties of the Jscript.js and stylessheet.css files separately, so that the processing of the files is complete.

Two, a reference to the embedded resource.
A reference to a resource in the AssemblyInfo.cs file.
In addition to setting the properties of the file, we also need to set a reference to the resource by opening the project's AssemblyInfo.cs file and adding the following two lines of code to the last side of the file:

[Assembly:webresource ("ServerControl.SelectCtlReSource.Stylesheet.css", "Text/css")]
[Assembly:webresource ("ServerControl.SelectCtlReSource.JScript.js", "Text/javascript")]
Note that it has two parameters, the first parameter it is a string, the overall meaning is the path of the file, in the format "namespace of the Reference class" + "Resource folder" + "file name", such as " ServerControl.SelectCtlReSource.Stylesheet.css ", Servercontrol is a namespace, Selectctlresource is the folder where CSS resides, STYLESHEET.CSS is the file name. The second parameter is the type that indicates the file, including "Img/gif", "Text/css", "text/javascript" and other commonly used file formats.

Note: When adding a reference, you need to add a reference to the AssemblyInfo.cs namespace using System.Web.UI.

So how do you use these two external files in a custom class?
third, use external CSS resources
we can use the Page.ClientScript.GetWebResourceUrl method to get the address of the external resource, the code is as follows:
String cssurl = Page.ClientScript.GetWebResourceUrl (this. GetType (),
"ServerContrAssemblyInfo.cs.SelectCtlReSource.Stylesheet.css");
Htmllink link = new Htmllink ();
Link. Href = Cssurl;
Link. Attributes.Add ("rel", "stylesheet");
Link. Attributes.Add ("type", "text/css");
PAGE.HEADER.CONTROLS.ADD (link);
explain the meaning of the code:
1, note the three parameters of Page.ClientScript.GetWebResourceUrl, the first one does not speak, for the type of resource. The second is more important, and its format is the same as the reference format above.
2, after getting to the URL of the resource, the following code is to register the CSS to the head of the page, this is not explained.

Four, using external JavaScript resources
referencing a JavaScript file is slightly different from a file that references CSS. Using a CSS file to get the path to an external CSS and then add the CSS to the head of the page through the Htmllink class, JavaScript can be registered directly through Registerclientscriptresource, with the sample code as follows:
Page.ClientScript.RegisterClientScriptResource (this. GetType (), "ServerControl.SelectCtlReSource.JScript.js");
Well, this completes the reference to the external resource in your custom control. Usually we see the page generated in theWebResource.axdafter adding a bunch of strings, the total sense of webresource will be a very complex thing, in fact, after understanding its rules, these things are easy to understand and remember, we do practice it.

The two files that we registered in the previous example will eventually generate code similar to the following in the HTML file:
<link rel= "stylesheet" type= "Text/css" href= "/webresource.axd?d= 21kpm8lphla-zr5ejp1pnghdv1cxwkqpw-qlxfdu3jwwmymuun49ab8qvx6fh66d6oqmw7wwdkixce8t4dofe0ype4vwtox9hda0plrejocbpgcflwup5cm6z Jgcujwigqlkwgk6_bv-90hwtf9daidiel5ab-0a7rac1uw5onioe2c70&t=634746559960937500 "/>

<script src= "/webresource.axd?d=8e4j6lzcj4mpilleetw_qnoqvym1ofcpjfvezi5e8b5ofi6ajjehddpnxnslv-fcvsoobv9rwpt_ sa1nvurxad42lny1&t=634751762881093750 "type=" Text/javascript "></script>

Webresource-asp.net custom controls referencing external resource methods

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.