设定控件获得焦点

今天看到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的客户端参考!

你可能感兴趣的:(控件)