Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm interested in building a custom web part for SP2010 that's editable by the page editor via the web part menu's "Edit Web Part" option; specifically, I'd like to start with this: http://blogs.msdn.com/b/ramg/archive/2009/04/21/implementing-a-simple-cross-site-collection-list-view-webpart.aspx

I'm not sure what type of project to choose in Visual Studio 2010 for that web part. I chose a Visual Web Part project, and it created some files. Where would the code from that example go? Here's the code Visual Studio auto generated in the VisualWebPart1.cs:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CrossSiteListView.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/CrossSiteListView/VisualWebPart1/VisualWebPart1UserControl.ascx";

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }
    }
}

Should I replace the above code with the code from that page? Your guidance on this will be much appreciated. Thanks :)

share|improve this question
2  
Visual web part project template is used to create web part with user control. In the provided example, the CrossSiteCollectionListViewWebPart uses ListViewByQuery control. In this case you can create Empty SharePoint project and, add Web Part Item and then copy code from example. Or you can add ListViewByQuery to your user control in the visual web part project and copy the data retrieve code to OnLoad method of your user control –  Alexander Oct 31 '11 at 8:52
    
Thanks, Alexander. I was wondering how they did that :) –  Alex C Oct 31 '11 at 13:08
add comment

1 Answer

up vote 2 down vote accepted

Generally, Visual web parts should be created if you want to fully utilize the designer capabilities in Visual Studio.(For e.g. to allow for features such as drag and drop as well as double clicking controls to wire up events).

A Visual web part implementation includes an ASP.NET user control, i.e. an .ascx file. All the UI controls and logic goes in user control.

So, if you want to continue with Visual web part approach, you should code in ascx.cs. In your case, you will find it with name VisualWebPart1UserControl.ascx.cs. If you want to use webcontrols like Textbox or Button, just drag and drop to .ascx file.

If you want to use the code as is (from the link you mentioned), Create standard WebPart instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.