ASP.NET Membership - 登录控件 - 文本框焦点

3

我知道这似乎是一个非常基本的问题,但我无法弄清如何在PageLoad上将焦点放在文本框上。

由于这是一个登录控件,我无法通过代码以我熟悉的方式对每个文本框进行单独控制。

是否有人知道如何给控件设置焦点?

史蒂夫

5个回答

5

根据您的要求进行调整...

protected void Page_Load(object sender, EventArgs e)
{
    string focus = "document.getElementById('" + Login1.ClientID + "_UserName').focus();";
    // the following assumes that your login page's type is 'Login' 
    // e.g. public partial class Login : Page ....
    ClientScript.RegisterStartupScript(typeof(Login), "uidFocus", focus, true);
}

2
< p >Code Poet 给出了正确的答案,但我不确定为什么它已被删除。< /p >
 string focus = "document.getElementById('" + Login1.ClientID + "_UserName').focus();";
ClientScript.RegisterStartupScript(typeof(<insert type your login page here, e.g. Login>), "uidFocus", focus, true);

这很好,谢谢。
史蒂夫

我认为Greg可能已经发布了更好的解决方案,所以我删除了。看起来不错,但是如果登录是唯一的控件,那么可能只有这个方案才行?很好奇想知道... - Sky Sanders

1

只需将登录控件设置为焦点,它就可以工作。

Login1.Focus();

或者

Form.DefaultFocus = Login1.ClientID;

编辑:或者,可以使用Login1.FindControl("UserName").Focus();Form.DefaultFocus = Login1.FindControl("UserName").ClientID;来更精确地传递焦点。

完整源代码:

<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Login ID="Login1" runat="server">
</asp:Login>
</form>

protected void Page_Load(object sender, EventArgs e)
{
    Login1.Focus();
}

在IE6和Firefox 3.6中都适用于我。


顺便提一下,调用.Focus()会导致WebResource.axd为我呈现以下javascript。当您在代码后端设置焦点时,将调用WebForm_AutoFocus('Login1')方法。

function WebForm_FindFirstFocusableChild(control) {
    if (!control || !(control.tagName)) {
        return null;
    }
    var tagName = control.tagName.toLowerCase();
    if (tagName == "undefined") {
        return null;
    }
    var children = control.childNodes;
    if (children) {
        for (var i = 0; i < children.length; i++) {
            try {
                if (WebForm_CanFocus(children[i])) {
                    return children[i];
                }
                else {
                    var focused = WebForm_FindFirstFocusableChild(children[i]);
                    if (WebForm_CanFocus(focused)) {
                        return focused;
                    }
                }
            } catch (e) {
            }
        }
    }
    return null;
}
function WebForm_AutoFocus(focusId) {
    var targetControl;
    if (__nonMSDOMBrowser) {
        targetControl = document.getElementById(focusId);
    }
    else {
        targetControl = document.all[focusId];
    }
    var focused = targetControl;
    if (targetControl && (!WebForm_CanFocus(targetControl)) ) {
        focused = WebForm_FindFirstFocusableChild(targetControl);
    }
    if (focused) {
        try {
            focused.focus();
            if (__nonMSDOMBrowser) {
                focused.scrollIntoView(false);
            }
            if (window.__smartNav) {
                window.__smartNav.ae = focused.id;
            }
        }
        catch (e) {
        }
    }
}
function WebForm_CanFocus(element) {
    if (!element || !(element.tagName)) return false;
    var tagName = element.tagName.toLowerCase();
    return (!(element.disabled) &&
            (!(element.type) || element.type.toLowerCase() != "hidden") &&
            WebForm_IsFocusableTag(tagName) &&
            WebForm_IsInVisibleContainer(element)
            );
}
function WebForm_IsFocusableTag(tagName) {
    return (tagName == "input" ||
            tagName == "textarea" ||
            tagName == "select" ||
            tagName == "button" ||
            tagName == "a");
}
function WebForm_IsInVisibleContainer(ctrl) {
    var current = ctrl;
    while((typeof(current) != "undefined") && (current != null)) {
        if (current.disabled ||
            ( typeof(current.style) != "undefined" &&
            ( ( typeof(current.style.display) != "undefined" &&
                current.style.display == "none") ||
                ( typeof(current.style.visibility) != "undefined" &&
                current.style.visibility == "hidden") ) ) ) {
            return false;
        }
        if (typeof(current.parentNode) != "undefined" &&
                current.parentNode != null &&
                current.parentNode != current &&
                current.parentNode.tagName.toLowerCase() != "body") {
            current = current.parentNode;
        }
        else {
            return true;
        }
    }
    return true;
}

+1 我喜欢这个。我要删除我的答案,因为这是显而易见的答案。(提示给混合大师...) - Sky Sanders
@Code Poet,不应该删除你的代码,它是唯一能正常工作的代码。 - Steve
抱歉,格雷格,我在匆忙删除我的答案时忘记提升你了。虽然我仍然喜欢这个答案,但你能确认它是否有效以及在什么条件下有效吗? - Sky Sanders
@code poet:这是代码,应该很容易验证。Steve,你是否在做与此不同的事情? - Greg

0

所有上述建议对我来说都没有起作用,除非使用window.setTimeout()方法。

System.Text.StringBuilder scriptLoader = new System.Text.StringBuilder();

scriptLoader.Append("var txtBox=document.getElementById('"+ this.myAppLogin.FindControl("UserName").ClientID+"');");
scriptLoader.Append("\n");
scriptLoader.Append("if (txtBox!=null ) window.setTimeout('txtBox.focus();', 0); ");

this.ClientScript.RegisterStartupScript(this.GetType(), "onLoadCall", scriptLoader.ToString(),true);

-1
请使用以下内容:
<form defaultfocus="Login1$UserName">

回答问题的第一步是阅读问题。 - Sky Sanders
@code poet:这不是一个很有建设性的批评。这个解决方案可以工作,但它有点脆弱,因为如果你改变标记,"Login1$UserName" 的名称可能会改变。 - Greg
@greg - 请检查答案的修订记录。仅仅因为某人用次标准的答案掩盖了一个非答案并不是我撤销踩的原因。 - Sky Sanders
@code poet:抱歉,我没有注意到答案已被编辑。 - Greg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接