在Windows应用程序中很容易控制控件的聚焦,但是在ASP.NET中并没有提供这样的功能,但是我们同样可以实现这样的功能,下面是本人整理的三种类似的方法,供大家参考。
方法1:设置服务器端控件的焦点
下面是用到的JavaScript代码。
1<script language="javascript">
2 var control = document.getElementById(<control name>);
3 if( control != null ){ control.focus(); }
4</script>
2 var control = document.getElementById(<control name>);
3 if( control != null ){ control.focus(); }
4</script>
这里写了一个SetFocusControl函数来封装上面的JavaScript代码,并且注册到页面上,注册到页面上使用的是Page.RegisterStartupScript 方法
其中的ControlName是你要获得焦点的控件的ID。
1Public Sub SetFocusControl(ByVal ControlName As String)
2 ' character 34 = "
3 ' 注意空格的书写这里用chr(34)
4 Dim script As String = _
5 "<script language=" + Chr(34) + "javascript" + Chr(34) _
6 + ">" + _
7 " var control = document.getElementById(" + Chr(34) + _
8 ControlName + Chr(34) + ");" + _
9 " if( control != null ){control.focus();}" + _
10 "</script>"
11 Page.RegisterStartupScript("Focus", script)
12End Sub
2 ' character 34 = "
3 ' 注意空格的书写这里用chr(34)
4 Dim script As String = _
5 "<script language=" + Chr(34) + "javascript" + Chr(34) _
6 + ">" + _
7 " var control = document.getElementById(" + Chr(34) + _
8 ControlName + Chr(34) + ");" + _
9 " if( control != null ){control.focus();}" + _
10 "</script>"
11 Page.RegisterStartupScript("Focus", script)
12End Sub
其中的ControlName是你要获得焦点的控件的ID。
------------------------------------------------------------------------
方法2: 设置服务器端控件的焦点
1Private Sub SetFocus(ByVal controlToFocus As Control)
2 Dim scriptFunction As New StringBuilder
3 Dim scriptClientId As String
4 scriptClientId = controlToFocus.ClientID
5 scriptFunction.Append("<script language='javascript'>")
6 scriptFunction.Append("document.getElementById('" & scriptClientId & "').focus();")
7 scriptFunction.Append("</script>")
8 RegisterStartupScript("focus", scriptFunction.ToString())
9End Sub
10
11Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
12 If (Page.IsPostBack = False) Then
13 SetFocus(TextBox1)
14 End If
15End Sub
16
2 Dim scriptFunction As New StringBuilder
3 Dim scriptClientId As String
4 scriptClientId = controlToFocus.ClientID
5 scriptFunction.Append("<script language='javascript'>")
6 scriptFunction.Append("document.getElementById('" & scriptClientId & "').focus();")
7 scriptFunction.Append("</script>")
8 RegisterStartupScript("focus", scriptFunction.ToString())
9End Sub
10
11Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
12 If (Page.IsPostBack = False) Then
13 SetFocus(TextBox1)
14 End If
15End Sub
16
方法3:设置服务器端控件的焦点
1Private Function SetFocus(ByVal controlToFocus As Control) As String
2 ' The SetFocusScript method set focus to a control on a page load
3 Dim sb As New StringBuilder
4 sb.Append(" <SCRIPT language='javascript'>")
5 sb.Append("document.getElementById('")
6 sb.Append(controlToFocus.ClientID)
7 sb.Append("').focus();</SCRIPT> ")
8 If Not Page.IsStartupScriptRegistered("focus") Then
9 Page.RegisterStartupScript("focus", sb.ToString())
10 End If
11End Function
2 ' The SetFocusScript method set focus to a control on a page load
3 Dim sb As New StringBuilder
4 sb.Append(" <SCRIPT language='javascript'>")
5 sb.Append("document.getElementById('")
6 sb.Append(controlToFocus.ClientID)
7 sb.Append("').focus();</SCRIPT> ")
8 If Not Page.IsStartupScriptRegistered("focus") Then
9 Page.RegisterStartupScript("focus", sb.ToString())
10 End If
11End Function
In Page_Load:
1Me.SetFocus(Me.txtUserName)