views:

4242

answers:

3

When creating web parts for Sharepoint, is it better to create an actual web part, or is using and ASP.NET User Control (.ascx) just as good?

I already know how to create the user controls that I need, so it seems like the extra effort of creating a web part is just unnecessary leg work.

What are the advantages of using a web part over just creating and ASP.NET user control?

+6  A: 

I am a big fan of user controls except for the simplest web parts. I create a webpart that instantiates and loads the user control. There are other tools out there that you can use to expose your user control, like smartpart, but I would suggest you wrap it up yourself, as it is a good learning experience. Once you have done it once, you basically have a template for any other webpart you want to create.

Good Luck!

AdamBT
+6  A: 

A bare ASP.NET ascx control would have to be added to a custom layout page. This limits the utility of the control a little as it cannot be added "just anywhere".

Having a webpart gives the flexibility of the control being added to the site multiple times in different locations or even multiple times on the same page with different properties.

As has been mentioned it is good to use CreateChildControls() to create the controls in the webpart and it is not that much of a big deal to code and package a webpart into a solution, making it worth the extra effort.

Webparts are also able to accept connections from "filter" webparts on the same page, giving additional flexibility to webparts compared to hosting ascx controls on the site.

When it comes to editors using the site, it makes a lot of difference for them to be able to add a webpart compared to editing a page layout, publishing it and then creating pages based on that page layout, so from the perspective of a site editor, the difference in usability is really quite large.

I recommend going even further and coding your webpart to use an xslt file to display the contents and making the location of that xslt a configurable property of the webpart. This really adds to the flexibility of your control.

Look at the Dataview webpart to see how much can be done with the addition of custom rendering.

Nat
by `LoadChildControls()`, you mean `CreateChildControls()`, correct?
vitule
Yeah, been a while.
Nat
A: 

Create Custom SharePoint WebParts, it is simple. Try this too, Create Custom SharePoint WebParts