Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在页面加载时将焦点设置在asp.net控件上_Javascript_Pageload_Clientscript - Fatal编程技术网

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>&nbsp;ft&nbsp;
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(); 
}