I know this seems to be a very basic questions but I can't figure out how to give the textbox the focus on Page开发者_JAVA技巧Load.
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
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);
}
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
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;
}
All above suggestions did not work for me without using 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);
Use the following:
<form defaultfocus="Login1$UserName">
精彩评论