ASP.NET Membership - Login Control - TextBox Focus

I know these are apparently very simple questions, but I can't figure out how to make the text box focus on PageLoad.

Since this is a Login control, I do not have individual control over each text field through the code, as I am used to it.

Does anyone happen to know how to focus on control.

Steve

+3
source share
5 answers

Adjust it to fit your requirements ...

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

Code Poet gave the correct answer, but I'm not sure why it was deleted.

 string focus = "document.getElementById('" + Login1.ClientID + "_UserName').focus();";
ClientScript.RegisterStartupScript(typeof(<insert type your login page here, e.g. Login>), "uidFocus", focus, true);

This works great, thanks.

Steve

+2
source

, .

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.


By the way, calling .Focus () causes WebResource.axd to display the following javascript for me. The method WebForm_AutoFocus('Login1')is called when you set focus in the code.

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
source

All of the above suggestions did not work for me without using the window.setTimeout () method.

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);
0
source

Use the following:

<form defaultfocus="Login1$UserName">
-1
source

Source: https://habr.com/ru/post/1744678/


All Articles