3

The code below has a link to cause a post back (asynchronous or not) followed by div with a top-padding of 5000 :) - and the new HtmlEditorExtender control found in the Ajax Control Toolkit (4.1.51116).

The Problem:

Clicking the link at the top of the page, causes the HtmlEditorExtender to steal the focus, scrolling all the way down to the bottom.

The Question:

Does anyone know if this is a known bug? Can anyone think of a work around?

I can't even think how to ever use this control on a page with any other controls that cause postbacks!

The Code:

    <div>
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:LinkButton runat="server">test asynch postback - PLEASE DONT SCROLL DOWN!! :)</asp:LinkButton>
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <div style="padding-top: 5000px">
             Nooooooooooooooooooooooooo!!!!
            <asp:TextBox ID="txtPageBody" TextMode="MultiLine" Width="200px" runat="server" />
            <act:HtmlEditorExtender ID="htmlPageBody" runat="server" TargetControlID="txtPageBody" >
                <Toolbar>
                    <act:Bold />
                </Toolbar>
            </act:HtmlEditorExtender>
        </div>
    </div>

HtmlEditorExtender should be added as a tag, but don't have the reputation :)

1
  • This is just a gueass, but the problem is not that you loose focus but that a full postback is happening, im wokring on a similar problem. Aug 20, 2014 at 0:24

3 Answers 3

2

override the function that is stealing the focus on your page, with the focus line removed:

if (Sys.Extended && Sys.Extended.UI && Sys.Extended.UI.HtmlEditorExtenderBehavior && Sys.Extended.UI.HtmlEditorExtenderBehavior.prototype && Sys.Extended.UI.HtmlEditorExtenderBehavior.prototype._editableDiv_submit) {
Sys.Extended.UI.HtmlEditorExtenderBehavior.prototype._editableDiv_submit = function () {
//html encode
var char = 3;
var sel = null;

if (Sys.Browser.agent != Sys.Browser.Firefox) {
if (document.selection) {
sel = document.selection.createRange();
sel.moveStart('character', char);
sel.select();
}
else {
sel = window.getSelection();
sel.collapse(this._editableDiv.firstChild, char);
}
}

//Encode html tags
this._textbox._element.value = this._encodeHtml();
};
}

from here:

http://ajaxcontroltoolkit.codeplex.com/workitem/27026

1
  • Yes this did the trick! Funnily enough I saw that page when I was googling, but didn't think it applied for my situation, but it does. So all I did was place the script above in a script tage at the bottom of my page, and postbacks now work! Yay Mar 12, 2012 at 11:53
0

Have you tried adding MaintainScrollPositionOnPostback to the Page directive?

1
  • Yes I have tried, but MaintainScrollPositionOnPostback has no effect. Mar 9, 2012 at 17:37
0

Don't know a proper fix fir this or the reason why it happens in the first place, but here's a dirty workaround.

1. Add this script

<script type="text/javascript" language="javascript">
    function pageLoad() {
        // if we did not redefined the '__doPostBack' method yet
        if (typeof window.__doPostBack_original == "undefined") {
            // save the original method
            window.__doPostBack_original = window.__doPostBack;
            // redefine it
            window.__doPostBack = function (eventTarget, eventArgument) {
                document.getElementById('<%=hdnLastFocus.ClientID %>').value = eventTarget;
                var retval = window.__doPostBack_original(eventTarget, eventArgument);
                return retval;
            };
        }

        window.onload = document.getElementById('<%=hdnLastFocus.ClientID %>').focus();
    }
</script>

2. Remove the UpdatePanel

3. Add this hidden field

<asp:HiddenField ID="hdnLastFocus" runat="server" />

Hope this helps... :)

P.S. The focus is stolen before the postback and then taken back after the postback returns, so if the editor control is far from the current focus, it looks ugly, because the screen jumps from one to the other and back again. But if your page is relatively small in height it may suite your needs.

1
  • Thanks hofnarwillie, your solution kinda works. This does cause the page to jump right up to the top, and not to the last position it seems, but I suppose I could save the scroll position and restore it again. I will leave this as unanswered for now, as I would like to know if there is anyone else that has a better solution. Mar 12, 2012 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.