给你一个通用的函数
<code>
Public Sub SetFormFocus(ByVal control As Control)
If Not control.Page Is Nothing And control.Visible Then
If control.Page.Request.Browser.JavaScript = True Then
Dim sb As New System.Text.StringBuilder
sb.Append("<SCRIPT LANGUAGE='JavaScript'>")
sb.Append("<!--")
sb.Append(ControlChars.Lf)
sb.Append("function SetInitialFocus() {")
sb.Append(ControlChars.Lf)
sb.Append(" document.")
Dim objParent As control = control.Parent
While Not TypeOf objParent Is System.Web.UI.HtmlControls.HtmlForm
objParent = objParent.Parent
End While
sb.Append(objParent.ClientID)
sb.Append("['")
sb.Append(control.UniqueID)
sb.Append("'].focus(); }")
sb.Append("window.onload = SetInitialFocus;")
sb.Append(ControlChars.Lf)
sb.Append("// -->")
sb.Append(ControlChars.Lf)
sb.Append("</SCRIPT>")
control.Page.RegisterClientScriptBlock("InitialFocus", sb.ToString())
End If
End If
End Sub
</code>