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.

Edit Mode on a WebPart


It is sometimes interesting to know if the view of the current WebPart is the display view or the edit view.

I’ve done this little tutorial to show one difference which occur when you work with a Publishing site or not.

Working most of the time with publishing , I’m use to check the status with this test


if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)

For example, the code below displays 2 lines. The first is always displayed and the second only on edit mode.


protected override void CreateChildControls()
{
string contenuPasSecret = "Je m'appelle Christopher.";
string contenuSecret = "J'ai 26 ans.";
Controls.Add(new LiteralControl(contenuPasSecret));
if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
Controls.Add(new LiteralControl(contenuSecret));
}

As you can see on the print screen below, only the first line is displayed.

When we edit the page, we see the second too.

Until there, all was ok!
Then I tried to use this WebPart on a “Team site” and I was surprised to see only one line on the edit mode!

So I’ve done some debug and I found that the error comes from the SPContext.Current.FormContext.FormMode , instead of sending me « Edit » , it send me « Invalid »!


So the tip to perform this check on a Team site is to use the current page’s WebPartManager . Let’s modify our code to have something like this.


protected override void CreateChildControls()
{
WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(Page);
string contenuPasSecret = "Je m'appelle Christopher.";
string contenuSecret = "J'ai 26 ans.";
Controls.Add(new LiteralControl(contenuPasSecret));
if ((SPContext.Current.FormContext.FormMode == SPControlMode.Edit)|| (wpm.DisplayMode.Name.Equals("Design")))
Controls.Add(new LiteralControl(contenuSecret));
}

Now, all is ok! We have our 2 lines displaying in edit mode on both a team site and a publishing site

Christopher.

Article published for the first time the 2nd of June 2010 on Areaprog