Javascript 在页面加载时将焦点设置在asp.net控件上
我想在加载页面时将焦点设置在控件上。 我写了这段代码,但不起作用Javascript 在页面加载时将焦点设置在asp.net控件上,javascript,pageload,clientscript,Javascript,Pageload,Clientscript,我想在加载页面时将焦点设置在控件上。 我写了这段代码,但不起作用 protected void setFocus(System.Web.UI.Control ctrl) { string s = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>"; Type csType = this.GetType
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);
}
请帮忙
编辑:
这是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
ft
在cs文件的代码隐藏中,我声明它与您提到的相同。您应该能够调用控件的方法 不需要那种Javascript
protected void Page_Load(object sender, EventArgs e)
{
txtHeightfeet.Focus();
}
你能在designer.cs:protected global::System.Web.UI.WebControls.TextBox txtheightfeets中发布txtheightfeets的HTML标记以及你如何在代码中声明它吗?你能在designer.cs:protected global::System.Web.UI.WebControls.TextBox txtheightfeets;可能重复的
protected void Page_Load(object sender, EventArgs e)
{
txtHeightfeet.Focus();
}