WebParts in MojoPortal

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.
1/26/2007 3:46:46 AM
Gravatar
Total Posts 8

WebParts in MojoPortal

Hello

I need help to add a userControl in a webpart . I tried to do that but i didn't success i do that in the sampleWebPart that was made in MojoPortal i need to know how can i do that if i have a useControl and i want to add it to WebPart what is the steps to do that.

Mahmoud El-Sobki

1/26/2007 5:32:05 PM
Gravatar
Total Posts 18439

Re: WebParts in MojoPortal

Hi Mahmoud,

The SampleExternalWebPart is a server control implemented by inheriting from WebPart so it cannot contain user controls, it can only contain other server controls.

However, any user control (.ascx) can be made into a WebPart by implementing IWebPart as we have done in SiteModuleControl which is the base class for mojoPortal modules. User controls that implement IWebPart can contain other user controls.

All the mojoPortal built in Modules can be used as WebParts and made available on the MyPage feature.

Hope it helps,

Joe
1/28/2007 2:12:28 PM
Gravatar
Total Posts 8

WebParts in MojoPortal

Hi Joe,

namespace SampleExternalWebPart
{

    public class SimpleWebPart : WebPart,IWebPart
    {       
        public SimpleWebPart()
        {                       
            this.Title = "Simple Web Part";
            this.Description = "Simple WebPart Description";
            Label l = new Label();
            l.Text = "sobkawy";
            this.Controls.Add(l);
 }

    .
    .
    .
    .
    .
    .
     
    }
}


I tried the way that you tell me before to inherit from IWebPart class to give me the ablitiy to add control or user control to a webpart but i didn't success with me, do you have another way to make it success

Mahmoud El-Sobki

1/28/2007 2:50:45 PM
Gravatar
Total Posts 18439

Re: WebParts in MojoPortal

You did not understand, you are still inheriting from WebPart. IWebPart is an interface already implemented in WebPart so it makes no sense to specify IWebPart in your example.

Don't start with SampleWebPart as it is a server control not a user control.

Just add a UserControl (.ascx) to your web project the usual way. So you will have SomeControl.ascx and SomeControl.ascx.cs. Look in the SomeControl.ascx.cs file and you will see

public class SomeControl : UserControl

change that to

public class SomeControl :UserControl, IWebPart

then implement the requirements for IWebPart

#region IWebPart Members

         private string m_title = String.Empty;
         private string m_subTitle = String.Empty;
         private string m_description = String.Empty;
         private string m_titleUrl = String.Empty;
         private string m_titleIconImageUrl = String.Empty;
         private string m_catalogIconImageUrl = String.Empty;


         // Title
         public string Title
         {
             get
             {
                 return m_title;
             }
             set
             {
                 m_title = value;
             }
         }
         //  Subtitle
         public string Subtitle
         {
             get
             {
                 return m_subTitle;
             }
             set
             {
                 m_subTitle = value;
             }
         }
         //  Description
         public string Description
         {
             get
             {
                 return m_description;
             }
             set
             {
                 m_description = value;
             }
         }
         //  TitleUrl
         public string TitleUrl
         {
             get
             {
                 return m_titleUrl;
             }
             set
             {
                 m_titleUrl = value;
             }
         }
         //  TitleIconImageUrl
         public string TitleIconImageUrl
         {
             get
             {
                 return m_titleIconImageUrl;
             }
             set
             {
                 m_titleIconImageUrl = value;
             }
         }
         //  CatalogIconImageUrl
         public string CatalogIconImageUrl
         {
             get
             {
                 return m_catalogIconImageUrl;
             }
             set
             {
                 m_catalogIconImageUrl = value;
             }
         }


         #endregion

Now you will have a UserControl which implements IWebPart and can be used as a WebPart and contain other UserControls

Hope it helps,

Joe

ps, a good place to get help with WebParts is the WebParts forum on asp.net
1/28/2007 4:20:23 PM
Gravatar
Total Posts 8

WebParts in MojoPortal

Hi Joe,

First i want to give you a special thank for your fast and great support in the site, i tried the way you told me before to add user control and put in it IWebPart interface into the user control.ascx.cs then i add the  control in the SampleWebPart class like this code :

 

The UserControl Code

namespace mojoPortal.Web.UI
{
    public partial class MyUserControl: System.Web.UI.UserControl,IWebPart
    {
        #region IWebPart Members

        private string m_title = String.Empty;
        private string m_subTitle = String.Empty;
        private string m_description = String.Empty;
        private string m_titleUrl = String.Empty;
        private string m_titleIconImageUrl = String.Empty;
        private string m_catalogIconImageUrl = String.Empty;


        // Title
        public string Title
        {
            get
            {
                return m_title;
            }
            set
            {
                m_title = value;
            }
        }
        //  Subtitle
        public string Subtitle
        {
            get
            {
                return m_subTitle;
            }
            set
            {
                m_subTitle = value;
            }
        }
        //  Description
        public string Description
        {
            get
            {
                return m_description;
            }
            set
            {
                m_description = value;
            }
        }
        //  TitleUrl
        public string TitleUrl
        {
            get
            {
                return m_titleUrl;
            }
            set
            {
                m_titleUrl = value;
            }
        }
        //  TitleIconImageUrl
        public string TitleIconImageUrl
        {
            get
            {
                return m_titleIconImageUrl;
            }
            set
            {
                m_titleIconImageUrl = value;
            }
        }
        //  CatalogIconImageUrl
        public string CatalogIconImageUrl
        {
            get
            {
                return m_catalogIconImageUrl;
            }
            set
            {
                m_catalogIconImageUrl = value;
            }
        }


        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

The SampleWebPart Code


namespace SampleExternalWebPart
{

    public class SimpleWebPart : WebPart
    {       

        public SimpleWebPart()
        {           
            this.Title = "Simple Web Part";
            this.Description = "Simple WebPart Description";
            MyUserControl my = new MyUserControl();
            this.Controls.Add(my);
 }
    }
}


and still not working ,i am sorry for asking you a lot of questions and thank you again.

Mahmoud El-Sobki

1/28/2007 5:04:02 PM
Gravatar
Total Posts 18439

Re: WebParts in MojoPortal

Hi Mahmoud,

The problem is its not a good idea to load a UserControl inside a Server Control

UserControls inherit from UserControl and have an associated .ascx file
Server Controls have no associated .ascx files and can be compiled into a dll

I meant for you to use the SomeUserControl.ascx as the WebPart instead of SampleExternalWebPart
I did not mean for you to try to use SomeUserControl.ascx inside SampleExternalWebPart because SampleExternalWebPart is a Server Control and you should not use User Controls inside Server Controls

I don't mean it is not possible to use a UserControl inside a WebPart Server control, its just a bad idea and I don't recommend it.

However if you really want to know how to do it, you must load User Controls by loading the .ascx file like this:

namespace SampleExternalWebPart
{

    public class SimpleWebPart : WebPart
    {      

        public SimpleWebPart()
        {          
            this.Title = "Simple Web Part";
            this.Description = "Simple WebPart Description";
            MyUserControl my = (MyUserControl)Page.LoadControl("~/PathTo/SomeControl.ascx");
            this.Controls.Add(my);
 }
    }
}

I do not recommend this but it should work.

It is ok to use Server Controls inside UserControls but in my opinon it is not a good idea to use User Controls inside Server Controls

Hope it helps,

Joe
1/28/2007 5:28:24 PM
Gravatar
Total Posts 18439

Re: WebParts in MojoPortal

To be even more clear about the difference between User Controls and Server Controls.

A User Control (.ascx) inherits from UserControl and is designed to be re-used within an application and is very easy to implement.

A Server Control inherits from WebControl and can be compiled into a dll and re-used in multiple applications by dropping the dll in the bin folder and referencing it.

This is why its bad to load a User Control inside a Server Control

SampleExternalWebPart inherits from WebPart which inherits from Part which inherits from Panel which inherits from WebControl
therefore SampleExternalWebPart is a Server Control and User Controls should not be loaded inside them

Of course nothing prevents you from loading a UserControl inside a Server Control but when you deploy the dll for the Server Control it needs to know the location of the User Control.

Any functionality that you implement inside a User Control can be implemented in a Server Control but it is more work to implement a Server Control. It is better to only include other Server Controls inside a Server Control and not use User Controls inside it.

I hope this makes sense and helps explain it.

Joe
1/28/2007 6:05:17 PM
Gravatar
Total Posts 8

WebParts in MojoPortal

Hi Joe,

I tried to use LoadControl method before but i didn't success and i tried it again and i didn't success

MyUserControl my = (MyUserControl)Page.LoadControl("~/Web/Controls/MyUserControl.ascx");

and this error appear Object reference not set to an instance of an object. that mean the path is wrong

and about using server control in webpart can you give me an example and if you have a solution for the virtual path to load the usercontrol tell me

 

Mahmoud El-Sobki

1/28/2007 6:16:00 PM
Gravatar
Total Posts 18439

Re: WebParts in MojoPortal


MyUserControl my = (MyUserControl)Page.LoadControl("~/Web/Controls/MyUserControl.ascx");

should probably be this:

MyUserControl my = (MyUserControl)Page.LoadControl("/Controls/MyUserControl.ascx");

or maybe this

MyUserControl my = (MyUserControl)Page.LoadControl("Controls/MyUserControl.ascx");
1/28/2007 7:21:02 PM
Gravatar
Total Posts 8

WebParts in MojoPortal

Hi Joe,

I tried the way you told me but i didn't work do you have another way to solve this problem.

Mahmoud El-Sobki

You must sign in to post in the forums. This thread is closed to new posts.