我知道这似乎是一个非常基本的问题,但我无法弄清楚如何将文本框的重点放在PageLoad上。
由于这是一个登录控件,因此我无法通过代码按照我习惯的方式单独控制每个文本框。
有人碰巧知道如何专注于控制。
史蒂夫
答案 0 :(得分: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);
}
答案 1 :(得分:2)
Code Poet给出了正确的答案,但我不确定为什么它已被删除。
string focus = "document.getElementById('" + Login1.ClientID + "_UserName').focus();";
ClientScript.RegisterStartupScript(typeof(<insert type your login page here, e.g. Login>), "uidFocus", focus, true);
这很有效,谢谢。
史蒂夫
答案 2 :(得分:1)
只需给出Login控件焦点即可。
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;
}
答案 3 :(得分: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);
答案 4 :(得分:-1)
使用以下内容:
<form defaultfocus="Login1$UserName">