WebPart selection error on page


Hello everyone,

Today I’ll talk about a mistake that I had with a custom webpart.

The webpart has custom properties that are retrieved during the OnInit event of webpart.


protected override void OnInit(System.EventArgs e)

{
// Check/Initialize custom properties
if (this.RowLimit <= 0)
this.RowLimit = 10;
}

Nothing complicated.

When creating the site, this webpart needed to be adding twice to the homepage.

No worries for the creation of the site and adding webparts. The rendering was also correct, in short, everything was good.

Except that (yes, it must be a reason for this post!) When we wanted to change the value of this property through the interface, a strange behavior appeared.

If we wanted to change the property for the first webpart, no worries, but when we wanted to change the property for the second webpart, we realized that this was the first that was impacted by the change!

Same scenario when we wanted to move the second webpart, regardless of the movement made, it was the first that was impacted!

After a phase of debugging the problem no longer appeared when the method “OnInit” was comment. Strange. By thinking a little I remembered that when using an override, there was usually a call to the base!

So I changed the function  the following way.


protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
// Check/Initialize custom properties
if (this.RowLimit <= 0)
this.RowLimit = 10;
}

And, problem solved!

In summary: the initial OnInit of a webpart must be preserved so that the recording in the WebPartManager page is correct and we keep the ability to have multiple instance of the webpart in the same page.

Christopher.

Advertisements
This entry was posted in SharePoint 2010 and tagged , . Bookmark the permalink.

One Response to WebPart selection error on page

  1. Sanjay Rimal says:

    Great.

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