Skip to main content

dotNET Custom Controls and Resource Scripts

Custom Controls are Web Controls, (TextFields, Labels etc) which are customized by you.

The Custom Controls can then added to your website by adding the assembly containing the Custom Control or used through Visual Studio ASPX editor by created adding the Custom Control to the ToolBox (this is done by Right-Clicking the ToolBox and selecting Choose Items... then select the locate of the Assembly containing the Custom Control.

An example of a very simple Custom Control implementation can be found here
Simple Custom Control (msdn)


Additionally you can embed some of you JScript code in a .js file instead of in the aspx page itself. This js file should be added to you Custom Control project as an Embedded Resource (This means the JScript code is embedded in the resultant assembly. When this assembly is referenced by a website project it will be able to access the JScript functionality (??).
To add the js file add it to the Custom Control Project as a Resource.
Add a new Resource file to the Project.
Add you js file to the project. Set the Property of the js file, "Build Action" to "Embedded Resource".
Add the js file to the Resource file using the Resource Designer, Choose Add Existing Item.. from the menu at the top of the Resource Designer.
Add a Line to the AssemblyInfo.cs file,
first include:

using System.Web.UI;

to the top of the file
[assembly: WebResource("WebControlLibrary1.Resources.JScript1.js", "text/javascript", PerformSubstitution = true)]


Now to enable accessing the JScript contents from and ASP.NET file you must Register it with your Custom Control, do this in the OnPreRender Overload method using

protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "jscriptcontrol", Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary1.Resources.JScript1.js"));
base.OnPreRender(e);
}


Now rebuild. Your JScript file is now embedded in the assembly and can be accessed from ASP.NET using when you add the Custom Control to the aspx page using

(open bracket)
cc1:WebCustomControl1 ID="WebCustomControl1_1" runat="server"

(close bracket)

Adding JScript files and other resources to an assembly ans accessing from ASP.NET
Adding and Accessing Embedded Resources
Adding Client Scripts to ASP.NET
http://msdn.microsoft.com/msdnmag/issues/06/05/CuttingEdge/default.aspx#S6

To test your embbedded resource is actually correct:
Drag and drop the Custom Control which references the JScript into a new aspx page.
Publish the website with the new aspx page.
Navigate to it's url in a browser, when it opens View-Source.
In the source locate the script src tag.

(start script tag)
src="/UseGalaxyWebControl/WebResource.axd?d=WS6VQ6ceILK4cpcSIBDTOA0gOitJm62vmYH2X7E8EUOB2p55z1WYfEnbnNTg7cQs-HZKaxcnh4sTcOoa0Og_HVTyCk7NIZnZpc-1MUQd7bw1&t=632974654934687088" type="text/javascript">
(end script tag)

Copy the link you find in here. Copy the link back into the browser.
Hit return and you should see the contents of the JScript file you embedded in the assembly.

/UseGalaxyWebControl/WebResource.axd?d=WS6VQ6ceILK4cpcSIBDTOA0gOitJm62vmYH2X7E8EUOB2p55z1WYfEnbnNTg7cQs-HZKaxcnh4sTcOoa0Og_HVTyCk7NIZnZpc-1MUQd7bw1&t=632974654934687088

http://localhost//UseGalaxyWebControl/WebResource.axd?d=WS6VQ6ceILK4cpcSIBDTOA0gOitJm62vmYH2X7E8EUOB2p55z1WYfEnbnNTg7cQs-HZKaxcnh4sTcOoa0Og_HVTyCk7NIZnZpc-1MUQd7bw1&t=632974654934687088
This works because the ASP.NET 2.0 has an assembly reflection type service WebResource.xad. This uses the url passed into the service to extract it from the assembly.
For more see this description
Using the WebResource.axd Handler with Embedded ASP.NET Resources (eggheadcafe.com)


So now you can drag and drop the Custom Control onto your ASPX page in Visual Studio designer. What good is that?
Your aspx page now has access to all your JScript functionality. You can call your JScript functions in the aspx page as normal using the tag.


Resource Caching:
So you've embedded the resource and added it to your aspx.
ASP.NET 2.0 also provides some caching functionality by default.
If the application is in Release (the web.config debug="false" set) all the resources are cached, meaning they will not be reloaded (GET) each time the page is POSTED as long as the resources have not changed.
In debug mode the resources are not cached, so they are always reloaded on page POST.
You'll see in the url for the resource, it contains a t=... this t is the time. This is what's used to compare the cached version of the resource and the latest version on the server.

Comments

Unknown said…
It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
dot net training in velachery

Popular posts from this blog

dotNET - Debugging

Debugging with .NET MSIL assemblies Visual Studio and debugging the CLR are different, I'll talk about both. MSIL Assemblies Assemblies compiled with .NET tools such as the CLR compiler are compiled into a file which contains MSIL (Microsoft Intermediate Language). At runtime the contents of the assembly are loaded into the CLR and ran as machine code. When you compile an assembly in debug a PDB file is generated alongside the DLL or EXE you've just created. The link between these 2 files is that the PDB contains the line numbers of the methods and classes as well as the file names of the original source code that created the assembly. When you launch the debugger in Visual Studio the assembly is loaded into the Debugger (similar to the CLR) along with the PDB file. The debugger now uses your PDB file contents to match the running code found in the assembly to locations in source files (hopefully in your present project). CLR CLR Inside Out (msdn magazine) .NET Framework Tools:...

Installer CustomAction, Debugging the CustomAction, InstallState

Custom Action The Custom Action is added to the Setup Project, select the Project node and hit the Custom Action button. This allows you add an Action to a particular phase in the Installation. But first you must create the Custom Action. To Add a Custom Action you must first have a Custom Action created, this is usually in the form of a Installer Class, this should be created in a seperate project, the Installer Class is actually one of the File Templates in the C# Projects. So it's File->New Project and select Visual C# Projects. Then add a Class Library, this will prompt you for the Class Library Types , select "Installer Class". Walkthrough - Creating Custom Action (msdn). Also here's a more comprehensive document on Setup/Installer implementations, it delves into the Registry etc Getting Started with Setup Projects (SimpleTalk). Visual Studio Setup Projects and Custom Actions (Simple Talk). Create your Installer Class and then add it as a Custom Action to the ...

dotNET - Use app.config ApplicationSettings and UserSettings

When using Settings in an Assembly or .exe you can use the Settings Designer to generate a config file using Settings. The Settings Designer provides a wrapper class which allows you to provide defaults and access the config data using Properties. But what if you're not working inside that Assembly or .exe? this presents a problem. If your loading the Assembly externally and want to access that Assembly's .config file you'll probably wish to use something in the System.Configuration namespace... unfortunately it's not of much use if you've created the .config file from the Settings Designer in Visual Studio!! This is because the Designer creates Sections and ApplicationSettings and UserSettings, the System.Configuration namespace does not provide a method to access these (it has a method to access AppSettings which are a different thing. Below I've written a workaround which locates the app.config and accesses the ApplicationSettings and UserSettings using XML i...