Creating a WebPart (beginner)

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
2/2/2007 8:24:00 AM
Gravatar
Total Posts 41

Creating a WebPart (beginner)

Hi!

I want to create a webpart that loads some strings from another SQL database and just lists them. I'm looking at the SimpleWebPart.cs example. Could I just open a new sqlconnection, read the data (OnLoad or OnInit?) and use writer.Write on each row?

What's the do's and dont's here?

Any help to send me in the right direction is appreciated.

Thanks
Emil

2/2/2007 8:52:10 AM
Gravatar
Total Posts 18439

Re: Creating a WebPart (beginner)

Hi Emil,

Yes you could do that but there are some other options and some pros and cons to consider.

There are 2 ways to make a WebPart as a UserControl or as a ServerControl

UserControls are easier to implement and can be re-used, but only easily within the same web application. If you have created a .ascx file it is a UserControl, it inherits from System.Web.UI.UserControl.

ServerControls are a little more work to implement because everything is implemented in code, you have a compiled class and no .ascx file. ServerControls inherit from System.Web.UI.WebControls.WebControl

To make a WebPart as a UserControl, you just create a UserControl in the normal way then implement IWebPart which is a very simple interface to implement.

To make a WebPart as a ServerControl, you create a class that inherits from System.Web.UI.WebControls.WebParts.WebPart. WebPart inherits from Part which inherits from Panel which inherits from WebControl, so it is a server control.

The SimpleWebPart.cs example is a server control. It compiles into the assembly SampleExternalWebPart.dll
To use this in mojoPortal you just have to include that dll in the bin folder then "install" it in the system using Admin > Install WebParts. However, keep in mind that this doesn't work in Medium trust because Medium trust doesn't allow the use of Reflection and we use Reflection to find and load WebParts from dlls in the bin.

The other option is to just create a UserControl.ascx that inherits from mojoPortal.Web.SiteModuleControl. SiteModuleControl already has implemented IWebPart which allows any module in mojoPortal to be used as a WebPart and made available on the MyPage feature from the Module Settings page.
Then you add this module using Admin > Feature Modules
Then you create instance of the Module in the Content Manager, click the gear next to the Instance Title and it takes you to the Module Settings, check the box to make it  "Available For MyPage" and now authenticated users can add it to their personalized pages under MyPage
This option works fine in Medium trust.

Hope it helps,

Joe

2/2/2007 10:16:48 AM
Gravatar
Total Posts 41

Re: Creating a WebPart (beginner)

Thanks for your lightning fast replies Joe (as always).

Do I need to recompile my webpart when installing new versions of mojoPortal?

"ServerControls are a little more work to implement because everything is implemented in code"
What exactly do you mean by this? It doesn't look much by looking in the SimpleWebPart.cs.

I'm not very familiar with the asp.net world, I'm a Win C# coder.

2/2/2007 10:27:47 AM
Gravatar
Total Posts 18439

Re: Creating a WebPart (beginner)

No, if your webparts are in separate dlls you don't need to re-compile them when upgrading mojoPortal.

ServerControls are a little more work because you have to take control of the Rendering, its not rocket science to build ServerControls, but UserControls are easier in the sense that you can use the Designer and you don't have to manage the rendering, you just add things to the Control tree and ASP.NET handles rendering for you.

Joe
2/5/2007 5:35:56 AM
Gravatar
Total Posts 41

Re: Creating a WebPart (beginner)

Thanks for your help Joe! It really was that easy! (I was a bit frightened at first when loading up the solution and browsing the solution tree ).
You must sign in to post in the forums. This thread is closed to new posts.