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:



//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; }
}


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

We look at WebPartUserControl.ascx.cs



public SearchWebPart WebPart
{
get;
set;
}

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

}


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:



this.WebPart.rowLimit


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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

1 comment on “SharePoint 2010 Using multiple instances of a custom Visual Webpart with properties

  1. Mustapha

    This article helps me to figure out my web part issue. Thanks a lot for the tips.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.