Click here to Skip to main content
15,883,901 members
Articles / Web Development / ASP.NET
Article

Adding Web resources to custom control in .net 2.0

Rate me:
Please Sign up or sign in to vote.
2.92/5 (4 votes)
18 Dec 2007CPOL 35.7K   667   20   2
This article dsecribes the method to embed web resources like js, css, image etc files to custom control

Introduction

In .net 2.0 it’s very simple to add web resource to the custom control. Following code will describe the method to add web resource to custom control.

Using the code

We have to follow the following steps to add web resource to custom control.

1. Add web resources like JavaScript, Style sheet, image file to your custom control project.
3. Set “Build Action” property to “Embedded Resource”. [Default value is set to “Content”]
4. Add reference to “System.Web” in your project.
5. Add the following to your custom control:

using System.Web.UI;
6. Add assembly reference of “Web Resources” to your project: [assembly: WebResource("ProjectName.ResourceFileName", "FileType")] For example:
[assembly: WebResource
("CustomCtrlWithWebResource.MyCssFile.css", "text/css")]
[assembly: WebResource
("CustomCtrlWithWebResource.MyJsFile.js", "text/javascript")]
7. Add following lines to add *.js file
//To add the JS file to the custom control
string jsResource = "CustomCtrlWithWebResource.MyJsFile.js";
this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), jsResource);
8. For other resources following code can be used:
//To add the CSS file to the custom control
string cssResource = "CustomCtrlWithWebResource.MyCssFile.css";
string cssResourceURL = Page.ClientScript.GetWebResourceUrl(this.GetType(), cssResource);
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssResourceURL;
cssLink.Attributes.Add("rel", "stylesheet");
this.Page.Header.Controls.Add(cssLink);
After adding above mentioned code, our “MyCustomCtrl.cs” file will have following content:
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;


[assembly: WebResource("CustomCtrlWithWebResource.MyCssFile.css", "text/css")]
[assembly: WebResource("CustomCtrlWithWebResource.MyJsFile.js", "text/javascript")]

namespace CustomCtrlWithWebResource
{
    class MyCustomCtrl: Control
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //To add the JS file to the custom control
            string jsResource = "CustomCtrlWithWebResource.MyJsFile.js";
            this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), jsResource);

            //To add the CSS file to the custom control
            string cssResource = "CustomCtrlWithWebResource.MyCssFile.css";
            string cssResourceURL = Page.ClientScript.GetWebResourceUrl(this.GetType(), cssResource);
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssResourceURL;
            cssLink.Attributes.Add("rel", "stylesheet");
            this.Page.Header.Controls.Add(cssLink);
        }
    }
}
You can add required functionalities to your custom control now and use the embedded resources like “javascript functions”,“cssClass” etc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionjavascript with custom control Pin
gaushiya31-Jul-12 23:21
gaushiya31-Jul-12 23:21 
Generalbase.OnPreRender(e); Pin
noname-20131-Dec-08 0:25
noname-20131-Dec-08 0:25 
base.OnPreRender(e) you should place in the end of function

http://www.codeproject.com/KB/cs/Embed_Js_Css_Image.aspx[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.