回发后维护标签索引

时间:2013-09-25 09:57:00

标签: c# asp.net

我有一个订单页面,其中有4个文本框位于ajax updatepanel中。所有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标记,因为它错误地说只有它们的实例。

因此,Order页面的代码中没有FormOrder或ScriptManagerOrder,但我想做一些foll。办法。 我怎么能这样做。

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

3 个答案:

答案 0 :(得分:4)

试试这个

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

答案 1 :(得分:0)

在js文件中添加以下脚本,名为38. focus.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

答案 2 :(得分:0)

对Tab使用服务器端控件不是一个好习惯。你为什么不使用一些jQuery / Bootstrap? 使用您当前的方法,您可以使用许多无用的帖子/回发来使您的服务器过载无用的工作。

相关问题