Page.SetFocus Page.SetFocus Page.SetFocus Page.SetFocus Method

定義

將瀏覽器焦點設為指定的控制項。Sets the browser focus to the specified control.

多載

SetFocus(Control) SetFocus(Control) SetFocus(Control)

將瀏覽器焦點設為指定的控制項。Sets the browser focus to the specified control.

SetFocus(String) SetFocus(String) SetFocus(String) SetFocus(String)

將瀏覽器焦點設為具有指定識別項的控制項。Sets the browser focus to the control with the specified identifier.

SetFocus(Control) SetFocus(Control) SetFocus(Control)

將瀏覽器焦點設為指定的控制項。Sets the browser focus to the specified 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

參數

control
Control Control Control Control

接收焦點的控制項。The control to receive focus.

例外狀況

當控制項不是 Web Form 網頁的一部分時,便呼叫 SetFocus(Control)SetFocus(Control) is called when the control is not part of a Web Forms page.

-或--or- SetFocus(Control) 事件之後,呼叫 PreRenderSetFocus(Control) is called after the PreRender event.

範例

下列程式碼範例會將焦點設定為網頁上的第二個文字方塊。The following code example sets the focus to the second text box on a Web page.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ 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使用方法,將指定的控制項設為瀏覽器所顯示網頁上的現用控制項。Use the SetFocus method to make the specified control the active control on the Web page displayed by the browser. 您必須先呼叫PreRender 方法,才能將頁面準備好在事件中呈現給用戶端。SetFocusThe SetFocus method must be called before the page is prepared for rendering to the client in the PreRender event.

注意

SetFocus方法僅適用于支援 ECMAScript 1.3 版或更新版本的瀏覽器。The SetFocus method will work only on browsers supporting ECMAScript version 1.3 or later.

SetFocus(String) SetFocus(String) SetFocus(String) SetFocus(String)

將瀏覽器焦點設為具有指定識別項的控制項。Sets the browser focus to the control with the specified identifier.

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

參數

clientID
String String String String

設定焦點的控制項 ID。The ID of the control to set focus to.

例外狀況

當控制項不是 Web Form 網頁的一部分時,便呼叫 SetFocus(String)SetFocus(String) is called when the control is not part of a Web Forms page.

-或--or- SetFocus(String) 事件之後,呼叫 PreRenderSetFocus(String) is called after the PreRender event.

範例

下列程式碼範例會將焦點設定為網頁上的第二個文字方塊。The following code example sets the focus to the second text box on a Web page.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ 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使用方法,將具有指定識別碼字串的控制項,設為瀏覽器顯示之網頁中的作用中控制項。Use the SetFocus method to make the control with the specified ID string the active control in the Web page displayed by the browser. 您必須先呼叫PreRender 方法,才能將頁面準備好在事件中呈現給用戶端。SetFocusThe SetFocus method must be called before the page is prepared for rendering to the client in the PreRender event.

注意

SetFocus方法僅適用于支援 ECMAScript 1.3 版或更新版本的瀏覽器。The SetFocus method will work only on browsers supporting ECMAScript version 1.3 or later.

適用於