我想在加载页面时将焦点设置在控件上。 我写了这段代码但没有用..
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 文件后面的代码中,我声明它与您提到的相同。
最佳答案
您应该可以调用 Focus()
控制方法。
不需要那个 Javascript。
protected void Page_Load(object sender, EventArgs e)
{
txtHeightfeet.Focus();
}
关于javascript - 将焦点放在页面加载上的 asp.net 控件上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5881826/