Using MVP with service locator and VISUAL WebPart

May 2, 2011 at 1:22 PM

I have succesfully used the MVP Pattern on a simple visual webpart, just by following the code on the sandbox example project, in that project you use a regular webpart instead of visual webpart, obviously because its sandboxed.

In the webpart code I added the following code

  public class SalesMarketingWebPart : WebPart, IViewSalesMarketing
    {
        /// <summary>
        /// Holds the instace of the listbox control on the user control
        /// </summary>
        private ListBox _listBox;


        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string AscxPath = @"~/_CONTROLTEMPLATES/Contoso.WebParts/SalesMarketing/SalesMarketingUserControl.ascx";

        /// <summary>
        /// Holds the instance to the presenter responsible to query the model service and to update the view.
        /// </summary>     
        private SalesMarketingPresenter _presenter;

        /// <summary>
        /// Holds the instance to the presenter responsible to query the model service and to update the view.
        /// Instantiates the presenter in case its not instantiated yet.
        /// </summary>
        private SalesMarketingPresenter Presenter
        {
            get
            {
                return _presenter ??
                       (_presenter = new SalesMarketingPresenter(this, new SalesMarketingService()));
            }
        }

        protected override void OnInit(EventArgs e)
        {
            _listBox = new ListBox();
            base.OnInit(e);
        }

        public SPListItemCollection SetSiteData
        {
            set
            {
                _listBox.DataSource = value;
                _listBox.DataBind();
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Control control = Page.LoadControl(AscxPath);
            Controls.Add(control);

            Presenter.SetSiteData();

            Controls.Add(_listBox);

            IErrorVisualizer errorVisualizer = new ErrorVisualizer(this);
            _presenter.ErrorVisualizer = errorVisualizer;

        }
    }

It works perfect, but I noticed that the user control is empty, what about if I want to drag and drop controls like gridview, dropdown list, etc in the user controls and use them from the webpart code to bind the data,  How can I do that?

I dont like the idea at all of building controls from scratch,

 

Please explain, thank you so much

Coordinator
May 2, 2011 at 2:13 PM

Yoiu can't use a user control unless you use the sandboxed visual web part.  See the powertools:

http://visualstudiogallery.msdn.microsoft.com/8e602a8c-6714-4549-9e95-f3700344b0d9/

May 2, 2011 at 2:19 PM

and if I do that, what part of my code I need to change?? if any?

 

May 3, 2011 at 7:37 AM

Ckeyser.

 

1. Why cant I use a user control ? It makes no sense, I am in a Farm solution I can use all types of code in farm solutions.

2. If I use a sandboxed visual webpart then I would have limitations.  I dont want to have limitations on the classes or methods I can use, I want to use the MVP Pattern but with a full visual webpart.

Right now its working, but I have to create the controls by code (thats the only thing I dont like).  I would like to do it using drag and drop of the controls.

 

 

thanks

Coordinator
May 3, 2011 at 12:52 PM

1. Why cant I use a user control ? It makes no sense, I am in a Farm solution I can use all types of code in farm solutions.

-> Misunderstood, it is fine to use a standard visual web part.

2. If I use a sandboxed visual webpart then I would have limitations. I dont want to have limitations on the classes or methods I can use, I want to use the MVP Pattern but with a full visual webpart.

-> true, there are pluses and minuses to using sandbox.  That is a bigger discussion.

Not sure about why you have to create the controls via code - the designer should work in tis case.  I don't know why you are experiencing these issues.  Generally you wire the view to the presenter in the code behind, which shouldn't impact the view.