Page.SetFocus 方法

定义

将浏览器焦点设置为指定控件。

重载

SetFocus(Control)

将浏览器焦点设置为指定控件。

SetFocus(String)

将浏览器焦点设置为具有指定标识符的控件。

SetFocus(Control)

将浏览器焦点设置为指定控件。

public:
 void SetFocus(System::Web::UI::Control ^ control);
public void SetFocus (System.Web.UI.Control control);
member this.SetFocus : System.Web.UI.Control -> unit
Public Sub SetFocus (control As Control)

参数

control
Control

要接收焦点的控件。

例外

controlnull

SetFocus(Control) 在控件不是 Web 窗体页的一部分时调用。

- 或 -

SetFocus(Control) 是在 PreRender 事件后调用的。

示例

下面的代码示例将焦点设置为网页上的第二个文本框。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Init(object sender, EventArgs e)
    {
        SetFocus(focusHere);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        SetFocus(focusHere)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>

注解

SetFocus使用 方法使指定的控件成为浏览器显示的网页上的活动控件。 SetFocus在准备页面以在 事件中PreRender呈现到客户端之前,必须调用 方法。

备注

方法 SetFocus 仅适用于支持 ECMAScript 1.3 或更高版本的浏览器。

适用于

SetFocus(String)

将浏览器焦点设置为具有指定标识符的控件。

public:
 void SetFocus(System::String ^ clientID);
public void SetFocus (string clientID);
member this.SetFocus : string -> unit
Public Sub SetFocus (clientID As String)

参数

clientID
String

要将焦点设置到的控件的 ID。

例外

clientIDnull

SetFocus(String) 在控件不是 Web 窗体页的一部分时调用。

- 或 -

SetFocus(String) 是在 PreRender 事件后调用的。

示例

下面的代码示例将焦点设置为网页上的第二个文本框。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Init(object sender, EventArgs e)
    {
        SetFocus(focusHere.ClientID);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        SetFocus(focusHere.ClientID)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>

注解

SetFocus使用 方法使具有指定 ID 字符串的控件成为浏览器显示的网页中的活动控件。 SetFocus在准备页面以在 事件中PreRender呈现到客户端之前,必须调用 方法。

备注

方法 SetFocus 仅适用于支持 ECMAScript 1.3 或更高版本的浏览器。

适用于