views:

188

answers:

3

More specifically, what do I need to know about doing this in Visual Studio 2008 that's different from VS2005? I have found a decent number of references for doing this kind of thing in VS2005, such as

but even the Microsoft reference doesn't seem to say much about VS2008. This question stems from one I asked earlier, found here.

Going one step further, once I manage to build one of these assemblies (presumably a DLL), what would someone else need to do with my DLL in order include it (as a single page, or maybe sub-section) in their ASP.NET web site?

EDIT: Still more searching has turned up this, which looks a lot (but not exactly) like what I am trying to figure out. Either:

  • I write the code for both the ASP.NET Web Site and Web Application, and then somehow hand that off to the owner of the main ASP.NET web site, or
  • I only write the code for the Application (thus creating the user control), and somehow the owner of the main ASP.NET web site shoves my user control into their site.

I hope this clarifies what I'm asking.

A: 

All someone needs to do to use your UserControl is to add a reference to the .dll in their project, whether it's a web application or a web site.

Usually the best idea is to add it to a folder relative to your project (I usually use "_Libraries"), as you'll need to deploy it to its final destination.

EDIT:

I think perhaps I misunderstood the question. ASCX files aren't easily portable, because the UI layout is contained in the markup, not in the code.

If you wish a user control to be distributable, you should build the layout entirely in code either by constructing it by adding components to the Controls() collection of your usercontrol during the lifecycle (usually Init() or CreateChildControls()), or by overriding the Render() method.

Then anyone can just add one of your usercontrols by using a <@Register TagPrefix="mycontrols" namespace="My.UserControls" Assembly="My.UserControls" /> directive, and a <% mycontrols:mydropdownlist %> asp tag to put it in their page.

womp
Once they have the reference to the DLL, how would they (for example) embed one of the DLL's web forms into a frame from the main website? Or (even simpler) construct a URL that points to a web form in the DLL?
Matt Ball
Matt - I updated my answer.
womp
A: 

This may seem like a silly question, but other than the nice design time interface, what benefits are you getting from having this as a User Control, rather than (the albeit more involved) method of writing a server control, which would give you exactly what you're asking for here - a simple way to deploy a control?

Assuming a CSS based approach to displaying the control, creating the actual control is fairly simple, using Panels for your containers (renders as a div), Labels, Literals, etc.

Some things need to be set at the correct point in the control lifecycle - e.g. you need to set the AssociatedControlId of a label to the ClientID of the input control, but this isn't set in CreateChildControls, you need to set it later.

Zhaph - Ben Duguid
For starters, I guess I really don't understand the differences between the various types of controls. However, you pretty much just mentioned two strong reasons to write a user control yourself - the nice design time interface, and that writing a server control is more involved.
Matt Ball
A: 

I finally figured out how to do exactly what I need. It's laid out pretty nicely here (even though that article claims to apply to VS2005), but in the end it boils down to just a few things:

  1. Create the user control inside an ASP.NET Web Site, NOT a web application.
  2. Give the user control declaration a class name that includes a custom namespace, as described at the end of step 1 within the linked page.
  3. After writing and testing the user control, create the DLL assembly as described in step 3
  4. Use it, as described in step 5.

The biggest problem that I had before was that I build the user control inside of an ASP.NET Web Application instead of a Web Site. I spent so many hours banging my head against this, hopefully this answer will save at least one other new ASP.NET dev the hours of slogging through the terminology and subtle differences. Cheers!

Matt Ball