HtmlForm.UniqueID 屬性

定義

取得指派給 HtmlForm 控制項的唯一程式設計識別項。

public:
 virtual property System::String ^ UniqueID { System::String ^ get(); };
public override string UniqueID { get; }
member this.UniqueID : string
Public Overrides ReadOnly Property UniqueID As String

屬性值

String

指派給 HtmlForm 控制項的唯一程式設計識別項。

範例

下列程式碼範例示範如何使用 UniqueID 屬性來擷取控制項的唯一 HtmlForm 識別碼。

<%@ 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 Button1_Click(object sender, System.EventArgs e)
  {
    
    // Write the form's UniqueID to the specified Label control.
    Label1.Text = "The HtmlForm control's UniqueID is "
                  + Form1.UniqueID + ".";
  }

  void Button2_Click(object sender, System.EventArgs e)
  {
    
    // Write the button's UniqueID to the specified Label control.
    Label2.Text = "This Button control's UniqueID is "
                 + Button2.UniqueID + ".";
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>HtmlForm UniqueID Property Example</title>

</head>

<body>

    <form id="Form1"
          runat="server">
    
    <h3>HtmlForm UniqueID Property Example</h3>        
  
    <asp:button id="Button1"
                text="Get the form's UniqueID" 
                onclick="Button1_Click"
                runat="server">
    </asp:button>
    
    <asp:label id="Label1"
               runat="Server">
    </asp:label>
    
    <br />
    
    <asp:button id="Button2" 
                text="Get this button's UniqueID"
                onclick="Button2_Click"
                runat="server">
    </asp:button>

     
  
    <asp:label id="Label2"
               runat="server">
    </asp:label>
    
  </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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    ' Write the form's UniqueID to the specified Label control.
    Label1.Text = "The HtmlForm control's UniqueID is " _
                  & Form1.UniqueID + "."
  End Sub
    
  Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    ' Write the button's UniqueID to the specified Label control.
    Label2.Text = "This Button control's UniqueID is " _
                  & Button2.UniqueID & "."
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>HtmlForm UniqueID Property Example</title>

</head>

<body>

    <form id="Form1"
          runat="server">
    
    <h3>HtmlForm UniqueID Property Example</h3>        
  
    <asp:button id="Button1"
                text="Get the form's UniqueID" 
                onclick="Button1_Click"
                runat="server">
    </asp:button>
    
    <asp:label id="Label1"
               runat="Server">
    </asp:label>
    
    <br />
    
    <asp:button id="Button2" 
                text="Get this button's UniqueID"
                onclick="Button2_Click"
                runat="server">
    </asp:button>

    &nbsp    ;
  
    <asp:label id="Label2"
               runat="server">
    </asp:label>
    
  </form>

</body>

</html>

備註

UniqueID使用 屬性可取得指派給 HtmlForm 控制項的唯一程式設計識別碼。 這個屬性會覆寫基底實作,當控制項包含在頁面以外的實作物件中 INamingContainer 時,傳回常數值,例如自訂伺服器控制項。 如果命名容器是頁面, HtmlForm 則會傳回控制項 ID 的 屬性值。

屬性 UniqueIDName 屬性一律會傳回相同的值。 當控制項在需要 HtmlForm 控制項 UniqueIDName 屬性相同之瀏覽器上轉譯時 HtmlForm ,這非常有用。 若要讓這些屬性保持同步, Name 屬性一律會傳回 屬性的值 UniqueID

注意

屬性 name 不是 XHTML 相容的屬性,而且預設不會由 ASP.NET 轉譯。

適用於

另請參閱