Naimish Pandya

Hey Folks,

Let’s see how we can add custom web part properties to a visual web part in SharePoint 2010.

Open Visual Studio 2010 -> File -> New -> Project -> Visual C# -> SharePoint -> 2010 and select Visual Web Part project template as below:

In SharePoint Customization Wizard, validate the SharePoint 2010 site URL as below:

Once Visual Studio 2010 open ups, it will look like below:

Open VisualWebPart1.cs file and add below code:

 

namespace AddCustomProperty.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/AddCustomProperty/VisualWebPart1/VisualWebPart1UserControl.ascx”;

private string _Name;
[WebBrowsable(true), WebDisplayName(“Name”), WebDescription(“My Name”),
Personalizable(PersonalizationScope.Shared), Category(“Custom Property”),
System.ComponentModel.DefaultValue(“”)]

public string Name
{
get { return _Name; }
set { _Name = value; }
}

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

View original post 79 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: