将焦点设置在页面加载的 ASP.NET 控件上
我想在加载页面时将焦点设置在控件上。 我写了这段代码但不起作用..
protected void setFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>";
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "focus", s);
}
并且 PageLoad 方法中的这一行:
this.setFocus(txtHeightfeet);
请帮忙。
编辑:
这是 HTML:
<input name="ctl00$MainContent$txtHeightfeet" type="text" maxlength="2" id="MainContent_txtHeightfeet" class="textEntry2" style="width:65px;" />
这是 aspx 代码:
<asp:TextBox ID="txtHeightfeet" runat="server" CssClass="textEntry2" MaxLength="2" Width="65"></asp:TextBox> ft
在 cs 文件后面的代码中,我声明它与您提到的相同。
I want to set focus on a control when page is loaded.
I wrote this code but not working..
protected void setFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>";
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "focus", s);
}
and this line in PageLoad method:
this.setFocus(txtHeightfeet);
Please help.
EDIT:
This is HTML:
<input name="ctl00$MainContent$txtHeightfeet" type="text" maxlength="2" id="MainContent_txtHeightfeet" class="textEntry2" style="width:65px;" />
This is aspx code:
<asp:TextBox ID="txtHeightfeet" runat="server" CssClass="textEntry2" MaxLength="2" Width="65"></asp:TextBox> ft
and in code behind cs file, i declared it the same as you have mentioned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够调用
Focus()
< /a> 控制方法。不需要 JavaScript。
You should be able to just call the
Focus()
method of the control.No need for that Javascript.