Friday, 12 June 2009

RichHtmlField Gaining Focus on Edit Issue

On a custom page layout, I have a few controls on the page, a few of these being RichHtmlField.

If I Edit page, focus is automatically set to the first RichHtmlField control on the page, IE will also scroll to the position where it's located and the cursor will blink in the contents of the field, the user can then start typing, however the changes when saved do not persist for the field.

We found however if you Edit Page, then click on the 'Edit Content' or click into the RichHtmlField, this will activate the editor, then any changes we made then get saved. This however is not ideal, as the control shouldn't automatically gain focus in the first place.

I have compared the OTB page layouts, and they do not have the same behavouir, When you click Edit Page, no control gains focus, you have to manually click into the RichHtmlField to edit content.

The question is why is it automatically gaining focus and how do we stop the RichHtmlField from gaining focus when the page is loaded in Edit mode?

Well I didn't find the answer, but I did come up with a work around, and that was to set focus to the 2nd text control in the page.

Why the 2nd I hear you ask?, well becuase the first control is the search input field.

Below is the javascript code that will achieve this:

function SetFocus()
{
var theForm = document.forms['aspnetForm'];
var elementCount = theForm.elements.length;

var controlcount=0;

for(var idx=0; idx {
var ele = theForm.elements[idx];

if ( ele.type == 'text' )
{
controlcount++;
if ( controlcount==2 )
{
ele.focus();
break;
}

}

}

}

No comments:

Post a Comment