SharePoint 2010 Using multiple instances of a custom Visual Webpart with properties

I already posted about the properties for your custom web part. Each webpart has a property toolbox by default. If you develop your own web part, you might want to make it more flexibel and provide a property toolbox for the endusers. But then it makes also sense, that you can use the webpart more than once at a page or in a website of a site collection.  In order to achieve this, i will go through my example web part where i used some settings. And finally it might help you, too.

We look at Webpart.cs:

[sourcecode language=”csharp”]

//Property for RowLimit
private int _rowLimit = 10;
[WebBrowsable(true),
WebDisplayName(“Row Limit”),
WebDescription(“Row Limit”),
Personalizable(PersonalizationScope.Shared),
Category(“Search Results”)]
public int rowLimit
{
get { return _rowLimit; }
set { _rowLimit = value; }
}

[/sourcecode]

We have a private variable and the rest you should know. It should not be static (_rowLimit).

We look at WebPartUserControl.ascx.cs

[sourcecode language=”csharp”]

public SearchWebPart WebPart
{
get;
set;
}

protected void Page_Load(object sender, EventArgs e)
{
this.WebPart = this.Parent as SearchWebPart;

}

[/sourcecode]

This is how we access in the user control the properties of the webpart. We make a connection to the search webpart and its properties and afterwords we can call them simply by using:

[sourcecode language=”csharp”]

this.WebPart.rowLimit

[/sourcecode]

That’s it. Hope it helps you.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

1 Comment

Leave a Reply