Embedding Resources in ASP.Net 2.0

  • embed resources

let’s start with the very basic if i tell you create the customize control (inherit the Textbox and create new Textbox MyTextBox) .

in other word you  can create customize textbox with three condition

1) Upper Case : when user selected Upper Case all enter input converted into Upper Character.

2) Lower Case : when user selected Lower Case all enter input converted into Lower Character.

3) None :  nothing to do.

now what can you do?

i am explain from my level.

1. i writen java script in the customize class.

2. i create java script and put that java script into my webapplication. i only written related function name in customize class.

above method is good but i face some problem which i explaining

1) in class i have written javascript which code is very long and unmanged from my site and the main thing is when small change occure i find into that much of code and replace and build that application.

2) in external javascript you added that javascript into the web application (this method is really good but some time if we not added that javascript then we are in trouble).

in both of the case java script is readable i have to find that thing one java script easy to manage and second thing not readable.

now i m check that thing in to the embed resource application.

  • Benifit Of embed resources :

1) manage all your dependencies into one single assembly like you identify the some stuff which are need in the application every time (like your company logo image,your company profile information).

2) especially javascript is not readable.

Steps :

1) create ClassLibrary Project In Visual Studio (Open –> File –> Project –> Class Library in C# Tag). give project Name : MyResource

2) Add Reference (go to Solution Explore –> Right Click On Reference –> select Add Reference).

2.1) select System.Web

3) change the Class Name MyTextBox

4) Inherite the TextBox Class in MyTextBox Class.

   1: public class MyTextBox : System.Web.UI.WebControls.TextBox

5) create Textbox which has different mode like when user select

1) upper then what ever they typed convered into upper

2) Lower then what ever they typed converted into lower

3) None nothing

now the main thing if you want to implement this then you want a create Java Script for this.

now add Java Script in to project.give a name MyJs.Js.

6) now Right Click On Added JavaScript and Select Property. change the Build Action –> Embedded Resource.

7) Add the Below Code in to MyTextBox Class.

   1: //enum for selection
   2:         public enum TextType
   3:         {
   4:             UpperCase,
   5:             LowerCase,
   6:             None
   7:         }
   8:
   9:         //property TextStyle
  10:         private TextType _TextStyle;
  11:         public TextType TextStyle
  12:         {
  13:             get { return _TextStyle; }
  14:             set { _TextStyle = value; }
  15:         }
  16:
  17:         public MyTextBox()
  18:         {
  19:
  20:         }
  21:
  22:         /// <summary>
  23:         /// 
  24:         /// </summary>
  25:         /// <param name="writer"></param>
  26:         protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
  27:         {
  28:             //add the attribute to in
  29:             if (this.TextStyle == TextType.LowerCase)
  30:             {
  31:                 writer.AddAttribute("onkeyup", "lower(this.id);");
  32:             }
  33:             else if (this.TextStyle == TextType.UpperCase)
  34:             {
  35:                 writer.AddAttribute("onkeyup", "upper(this.id);");
  36:             }
  37:             base.AddAttributesToRender(writer);
  38:         }
  39:
  40:         protected override void OnPreRender(EventArgs e)
  41:         {
  42:             //Register Java Script
  43:             Page.ClientScript.RegisterClientScriptInclude("MyTextBox", Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyResource.MyJS.js"));
  44:             base.OnPreRender(e);
  45:         }

8 ) now in the AssemblyInfo File Add the WebResource.

   1: //Add Name Space using System.Web.UI;
   2: //your NameSpace 
   3: [assembly: WebResource("MyResource.MyJS.js", "text/javascript", PerformSubstitution = true)]

9) Now Build Project then Create WebApplication (asp.net using C#) or Add New web Site In Existing Project. here i added new web site in Existing project.

10) Add MyTextBox Dll Reference in to Newly Created WebSite.

11) Add MyTextBox In to WebPage. Set the property

12) Run Web Site And Check.

how to retriving resource (ImageFile,html file) from the embeding resource ?

steps :

1) Add Image In to your exiting Class Library Project (Image Name :MyImage.Jpg).

2) now Right Click On Added JavaScript and Select Property. change the Build Action –> Embedded Resource.

3) now in the AssemblyInfo File Add the WebResource.

   1: [assembly: WebResource("MyResource.MyImage.jpg", "img/jpg")]

4) Build the Project. and go to the WebSite Project.

5) in the web site project put the image control.

6) now in the page load event get the myimage from the dll file.

   1: //the first argument type --> give the your class name
   2:       //second is resource name
   3:       Image1.ImageUrl =
   4:            Page.ClientScript.GetWebResourceUrl(typeof(MyResource.MyTextBox), "MyResource.MyImage.jpg");

7) check code.

Now the interesting thing check the page source.

Thnx

Advertisements

3 comments

  1. hi i like u r approch
    Thanx

  2. hi Mayur,
    Thanks For Your Motivational Comment.

    Have a gr8 day ahead.

  3. Hi
    This is an awesome way to hide javascript .Fantastic approach i liked
    Thanks for such a great post .
    Very Good Effort Hats Off .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s