0

ajax updatepanel 内にある 4 つのテキスト ボックスを含む注文ページがあります。4 つすべてに TextChanged イベントがあります。このページのどのコントロールにも TabIndex プロパティが設定されていません。textbox1 にテキストを入力して Tab キーを押すと、ポストバックが発生しますが、次のフォーカスは textbox2 にありません。代わりに、フォーカスはページにあります。すべてのテキストボックスについても同様です。

この注文ページは、マスター ページを使用します。

マスター ページ:

<form id = "form1" runat="server">
<asp:ScriptManager ID="ScriptManager1 " runat="server" />

注文ページ:

<asp:content id ="bodycontent" contentplaceholderID="maincontent" runat="server">
// 4 text boxes

</asp:content>

別のフォームまたは scriptmanager タグを注文ページに追加することはできません。それらのインスタンスしか存在できないというエラーが表示されるからです。

したがって、注文ページのコード ビハインドには FormOrder や ScriptManagerOrder はありませんが、何とかしたいと思います。仕方。これどうやってするの。

protected void textbox1_TextChanged(object sender, EventArgs e)
{
   //someFunction();
TextBox tb = (TextBox)FormOrder.FindControl("textbox2");
ScriptManagerOrder.SetFocus(tb);
}
4

3 に答える 3

4

これを試して

protected void textbox1_TextChanged(object sender, EventArgs e)
{
   //someFunction();
   TextBox tb = (TextBox)FormOrder.FindControl("textbox2");
   tb.focus();
}
于 2013-09-25T10:01:19.797 に答える
0

タブにサーバー側のコントロールを使用することはお勧めできません。jQuery/Bootstrap を使ってみませんか? あなたの現在のアプローチでは、多くの役に立たない投稿/ポストバックがサーバーを無駄な作業で過負荷にしています。

于 2013-09-25T11:29:04.903 に答える
0

focus.js という名前の js ファイルに次のスクリプトを追加します。

var lastFocusedControlId = "";

function focusHandler(e) {
    document.activeElement = e.originalTarget;
}

function appInit() {
    if (typeof(window.addEventListener) !== "undefined") {
        window.addEventListener("focus", focusHandler, true);
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}

function pageLoadingHandler(sender, args) {
    lastFocusedControlId = typeof(document.activeElement) === "undefined" 
        ? "" : document.activeElement.id;
}

function focusControl(targetControl) {
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        var focusTarget = targetControl;
        if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined")) {
            oldContentEditableSetting = focusTarget.contentEditable;
            focusTarget.contentEditable = false;
        }
        else {
            focusTarget = null;
        }
        targetControl.focus();
        if (focusTarget) {
            focusTarget.contentEditable = oldContentEditableSetting;
        }
    }
    else {
        targetControl.focus();
    }
}

function pageLoadedHandler(sender, args) {
    if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
        var newFocused = $get(lastFocusedControlId);
        if (newFocused) {
            focusControl(newFocused);
        }
    }
}

Sys.Application.add_init(appInit);

以下のように Scriptmanager を使用して参照します。

<ajax:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <ajax:ScriptReference Path="~/Js/FixFocus.js" />
        </Scripts>
    </ajax:ScriptManager>

詳細については、以下のリンクをご覧ください。

http://couldbedone.blogspot.in/2007/08/restoring-lost-focus-in-update-panel.html

于 2013-09-25T10:21:49.040 に答える