How to create a WebPart?

G

Guest

I create a UserControl (.ascx), and then change it in code to:
Inherits System.Web.UI.WebControls.WebParts.WebPart
expecting that it will now be treated as a WebPart. But this immediately
generates 2 errors. How does one go about creating a WebPart by inheriting
as above?

(I know that I can drag a UserControl into a WebPartZone and it will be
wrapped in a GenericWebPart.) Thanks

Jav
 
T

tdavisjr

So what are you asking? If you know a usercontrols is going to get
wrapped in a GenericWebPart why are you trying to inherit from
WebParts.WebPart

If you want to inherit from this class, then you may have to write a
custom control.

I don't know if implementing the IWebPart interface would give you the
same errors; but you can try it an see.
 
G

Guest

The reason I am asking is because the docs suggest that is a "better" way of
doing things. Unfortunately their examples do not explain much.
Jav
 
G

Guest

One simple fact is that you can't use the visual designer when creating
WebParts, because webparts can't inherit directly from the .NET classes. So
you have to create and configure all controls in the webpart through code,
and output the controls and graphic through either the CreateChildControls
method or the Render method (though the first is the easiest to use).

So your trouble might be that you created your control using the designer
and then try to let the class inherit from
System.Web.UI.WebControls.WebParts.WebPart which then won't work.

One fix is to rewrite the whole webpart in code.

This article also might be of help (though it is written for SharePoint
WebParts): http://www.devx.com/dotnet/Article/26732
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top