环境是VS2005,主要想要的效果就是页面刚打开时光标自动聚焦到Button控件上。用以下的代码不成功,大家帮忙看一下是哪里的问题?
一、ASPX页面:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jstest.aspx.cs" Inherits="jstest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>   
</head><body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width: 65px">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                <td style="width: 100px">
                    <asp:Button ID="btnOK" runat="server" Text="Button" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>二、CS代码using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class jstest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 在这里用的2005里的新增方法。之前也用过2003的方法,也是不行
            ClientScriptManager objCSM = Page.ClientScript;
            string jsBlkName = "testabc";            if (!objCSM.IsClientScriptBlockRegistered(jsBlkName))
            {
                string js = " <script language =javascript> ";
                js += " function setDefaultFocus() ";
                js += " { ";
                js += " document.all.btnOK.focus(); ";
                js += " } ";
                js += " </script> ";                objCSM.RegisterStartupScript(this.GetType(), jsBlkName, js);
            }        }
        
        
    }}三、运行时页面和JavaScript均没有报错,可是光标却没有聚焦到btnOK按钮上。查看网页源代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
无标题页
</title></head><body>
    <form name="form1" method="post" action="jstest.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTkwNjc4NTIwMWRkzlE0+7+SMt36jVOUgkDRxnaX69c=" />
</div>    <div>
        <table>
            <tr>
                <td style="width: 65px">
                    <input name="TextBox1" type="text" id="TextBox1" /></td>
                <td style="width: 100px">
                    <input type="submit" name="btnOK" value="Button" id="btnOK" /></td>
            </tr>
        </table>
    
    </div>
    
<div> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLc3beWCALs0bLrBgLdkpmPATNr0yucRQlumCJ9SxZWZgw9kA7U" />
</div>
 <script language =javascript>  function setDefaultFocus()  {  document.all.btnOK.focus();  }  </script> </form>
</body>
</html>

解决方案 »

  1.   


    方法setDefaultFocus有了,但没看到你调用的地方!
    不用function,直接一句话: document.all......;
      

  2.   

    感谢1楼,去掉function之后就可以了。还想问一下,如果要是保留function的话,在哪里里用什么方式调用setDefaultFocus方法呢?最好能用代码描述一下。
      

  3.   

    直接使用:
    if (!objCSM.IsClientScriptBlockRegistered(jsBlkName))
    {
      string js = " <script language =javascript> ";
      js += " document.getElementById('btnOK').focus(); ";
      js += " </script> ";  objCSM.RegisterStartupScript(this.GetType(), jsBlkName, js);
    }
      

  4.   

    if (!objCSM.IsClientScriptBlockRegistered(jsBlkName))
    {
      string js = " <script language =javascript> ";
      js += " setDefaultFocus();";
      js += " </script> ";  objCSM.RegisterStartupScript(this.GetType(), jsBlkName, js);
    }
      

  5.   

    <script language =javascript>  
    function setDefaultFocus()  
    {  
    document.all.btnOK.focus();  
    }  
    setDefaultFocus();
    </script>
      

  6.   

    其实更简单点实现,只要在Page_Load事件中使用 Page.SetFocus("你的控件名")就可以了