How to create redistributable user control in DLL form with embedded images, javascripts, style sheets? - asp.net

I've developed a most reusable ASP.Net WebUserControl for our company's web applications. I'm now going to make it as a redistributable DLL according to this msdn topic. Since the WebUserControl comes with some JavaScript(s), CSS(s) as well as images. I'm asking how can I bundle them all together to form a single DLL?
Our development environment is Visual Studio 2008, .Net Framework 3.5
Please kindly advise!
Thanks!
William

You could bundle the non source code parts in a resource file and reference them through there in your ASP.NET code. You could add to the default resource file (.resx) or add your own..

I haven't actually tried this but I think you could embed the user controls, JS files, etc into the output assembly of your reusable library project. Basically in the Properties dialog you'd set the "Build Action" to "Embedded Resource" for those files instead of "Content".
Then the consuming application would implement and register a VirtualPathProvider to tell ASP.NET to look for user controls inside the assembly.
Here's a similar question dealing with loading MVC Views from a DLL, same idea: Using VirtualPathProvider to load ASP.NET MVC views from DLLs
And this: http://www.wynia.org/wordpress/2008/12/aspnet-mvc-plugins/

Related

Reference aspx pages from different web project

I have an ASP .NET web form solution with multiple web projects. There is one core web project and each of the other web project work as pluggable modules.
I have written a post build event to copy aspx/ascx files and *.dll of child web projects to the core web project directory.
The issue is - I am not able to call child project aspx pages from the core project. I get "Could not load type" error.
Is it possible to use aspx pages in different project if i refer that webproject dll and copy the aspx files to the main application? What am i doing wrong here. I would greatly appreciate any pointer in the right direction. Thank you.
Sanjay
I found answer to this. The key is to define you assemblies in the web.config file and then define a probing path. Following is the link with details-
http://weblogs.asp.net/chrismoseley/archive/2008/10/28/shared-assemblies-without-the-gac.aspx

Generate partial class for ASP.NET Web Forms when importing into a project

I've created a new ASP.NET Web Site in VS2008. I'm attempting to rebuild an old VS.NET application (.NET 1.0). I imported all of the WebForms from the old project (Add Existing items) and I ended up with a ton of errors like:
"The name 'lblMessage' does not exist in the current context"
The partial classes in this project don't have the object definitions for each of the UI elements. I'm hoping there's a way to generate these without having to try to hand-code 500 object definitions.
I ultimately found a workaround for this issue. By manually creating the designer pages ('mypage.aspx.designer.cs') file and then performing an edit on the markup page ('mypage.aspx') Visual Studio will automatically rebuild all the references.
Try to use ConvertToWebApplication in Context Menu , WebApplication project file.
This will Generated aspx.designer.cs with declaration for controls.

Is dynamic compilation in a 'ASP.NET Web Application' possible?

Can I somehow utilize the App_code folder in a web application project to compile code on the fly? It'd be great for plugins. Recently Rob Conery demonstrated its use in his talk at MIX 09 in a ASP.NET MVC app. I tried to do the same in a web app but I can't access the classes under App_Code from anywhere else. But if Rob was able to do it in an MVC app, it should be doable in a web application too. After all ASP.NET MVC IS a ASP.NET Web Application under the covers.
If you add a code file to the App_Code folder, it should be compiled and available from a code-behind file for a control, or another code file in the App_Code file.
I don't think you'll be able to access it directly from a compiled assembly, since the compiler won't be able to find that reference at compile time.
You'll also need to be aware that App_Code is compiled into a different assembly than your code-behind code, so you can't access internal code across the different assemblies.

ASP.NET 2 projects to share same files

Well as the title says i have 2 web projects that have the same css files, master page and some resource files, how could i share those? atm i have 2 copies of them...
Solution 1: bind them into separate assemblies in share it across your projects.
Reference:
Building Re-Usable ASP.NET User Control and Page Libraries with Visual Studio 2005
How to embed resources in ASP.NET 2.0 assemblies
Here is an article that describes how to share pages and user controls between applications : HOW TO: Share ASP.NET Pages and User Controls Between Applications by Using Visual Basic .NET. It's solution depends on separate virtual directory that holds the shared files.
And in this article Scott Guthrie describes how to build reusable web assemblies. It's solution depends on making separate assembly by shared files.
In the second project choose "Add existing file", select the file, click on the arrow on the add button and select "add as link".

Is it possible to add a web user control to a class library?

I'm looking at building some web user controls with an eye toward re-use, but I can't seem to add a Web User Control in my class library in VS2008. Is there a way to work around this problem, or is there a better approach to creating reusable controls?
You can create either Web User Controls or Web Custom Controls that encapsulate the functionality you need. The main difference between the two controls lies in ease of creation vs. ease of use at design time.
You should maybe consider creating a Web Custom Control library. There is a walkthrough for creating a web custom control using the Web Control Library template.
According to the MSDN article "Recommendations for Web User Controls vs. Web Custom Controls" these are the differences between the two types of controls:
Web user controls are easy to make,
but they can be less convenient to use
in advanced scenarios. You develop Web
user controls almost exactly the same
way that you develop Web Forms pages.
Like Web Forms, user controls can be
created in the visual designer, they
can be written with code separated
from the HTML, and they can handle
execution events.
However, because Web
user controls are compiled dynamically
at run time they cannot be added to
the Toolbox, and they are represented
by a simple placeholder glyph when
added to a page. This makes Web user
controls harder to use if you are
accustomed to full Visual Studio .NET
design-time support, including the
Properties window and Design view
previews.
Also, the only way to share
the user control between applications
is to put a separate copy in each
application, which takes more
maintenance if you make changes to the
control.
Web custom controls are compiled code,
which makes them easier to use but
more difficult to create; Web custom
controls must be authored in code.
Once you have created the control,
however, you can add it to the Toolbox
and display it in a visual designer
with full Properties window support
and all the other design-time features
of ASP.NET server controls.
In addition, you can install a single
copy of the Web custom control in the
global assembly cache and share it
between applications, which makes
maintenance easier. For more
information see global assembly cache.
Follow the following steps (from this post by Phil Haacked):
Close VS.NET 2005.
Open the directory C**:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp** (assuming a default installation of VS.NET).
Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.
Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.
Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.
Now “Web User Control” should be an option when you select Add | New Item.
Reference: http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx
As platte's link mentions, if you're going for reuse then Web User Controls aren't very good. The ascx file has to be manually copied to every project you want to use them in, or you have to hack your way around that.
It's better to use System.Web.UI.WebControls.WebControl which is what you get when you add an "ASP.NET Server Control". These are designed for reuse. If one of the existing framework controls fits the bill for the most part and you just need to extend the functionality of it, then add an "ASP.NET Server Control" and change it to inherit from Panel or Menu or whatever.
If you're still determined to get reusable Web User Controls to work, then this article by The Gu should set you on the right path.
There is a project template called "ASP.NET Server Control" that I assume you can use...
--larsw
You can do anything in a class library.
Add reference to System.Web
Create your new Control class that inherits
from WebControl or HtmlControl or
whatever.
That's it. You now have a reusable control for ASP.NET.
You could do some special things like add attributes to your class and properties, but they are really not needed.
[DefaultProperty("Text")]
[Category("...")]
[DefaultValue("")]
You could using virtual path providers but you you should consider whether it really is worth your wile. Consider this codeproject article on the subject.

Resources