views:

246

answers:

4

I know this seems to be a very basic questions but I can't figure out how to give the textbox the focus on PageLoad.

Since this is a Login Control, I have no individual control over each textbox thru code the way I am used to it.

Does anyone happen to know how to give focus to the control please.

Steve

A: 

Use the following:

<form defaultfocus="Login1$UserName">
Blend Master
the first best step to answering a question is to read the question.
Sky Sanders
@code poet: That's not a very constructive critcism. This solution would work, but it's somewhat fragile because the name "Login1$UserName" could change if you change the markup.
Greg
@greg - check the revisions of the answer. Just because dude masked a non-answer with a substandard answer is not cause for me to revert downvote.
Sky Sanders
@code poet: Sorry, I didn't notice that the answer was edited.
Greg
+3  A: 

Adjust this to fit your requirments...

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);
}
Sky Sanders
A: 

Just give the Login control focus and it works.

Login1.Focus();

or

Form.DefaultFocus = Login1.ClientID;

Edit: Alternatively, Login1.FindControl("UserName").Focus(); or Form.DefaultFocus = Login1.FindControl("UserName").ClientID; can be used for more precise delivery of the focus.


Full source:

<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();
}

That works for me in both IE6 and Firefox 3.6.


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

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;
}
Greg
+1 i like this. am deleting my answer because this is the obvious answer. (hint to blendmaster...)
Sky Sanders
None of those 2 worked for me.
Steve
@Code Poet, should not have deleted yours, it is the only one working.
Steve
@Steve - ok ;-)
Sky Sanders
oops, sorry greg, forgot to up you in my haste to delete my answer. While I still like this answer, can you confirm that it works and under what conditions?
Sky Sanders
@code poet: There's the code, it should be easily verifiable. Are you doing something different than this, Steve?
Greg
+2  A: 

Code Poet gave the correct answer but I am not sure why it has been 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, thank you.

Steve

Steve
+1 for credit ;-) I thought that maybe Greg had posted a better solution so I deleted. It looks good, but perhaps only if the login is the only control? curious to know...
Sky Sanders

related questions