Wind-Eagle

No pain,no gain!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

设定控件获得焦点

Posted on 2008-01-23 13:44  Andrew Yin  阅读(208)  评论(0)    收藏  举报
今天看到AJAX ControlToolKit的源码中得到一个类,代码如下:
using System;
using System.Web.UI;

namespace AjaxControlToolkit
{
    
public static class Utility
    
{
        
public static void SetFocusOnLoad(Control control)
        
{
            
if (control == null)
            
{
                
throw new ArgumentNullException("control""Control cannot be null!");
            }


            
string script =
                
"(function() {\n" +
                    
"var fn = function() {\n" +
                        
"var control = $get('" + control.ClientID + "');\n" +
                        
"if (control && control.focus) { control.focus(); }\n" +
                        
"Sys.Application.remove_load(fn);\n" +
                    
"};\n" +
                    
"Sys.Application.add_load(fn);\n" +
                
"})();\n";
            ScriptManager.RegisterStartupScript(control.Page, control.GetType(),
                control.ClientID 
+ "_SetFocusOnLoad", script, true);
        }

    }

}

我觉得用这个代码设定焦点很不错.
有关Sys.Application的用法可以参考AJAX的客户端参考!