共用方式為


HtmlInputControl.Name 屬性

定義

取得或設定 HtmlInputControl 控制項的唯一識別項名稱。

public:
 virtual property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
public virtual string Name { get; set; }
member this.Name : string with get, set
Public Overridable Property Name As String

屬性值

字串,表示 UniqueID 屬性的值。

範例

下列程式碼範例示範如何使用 Name 屬性來判斷已按一下的 HtmlInputControl 名稱。


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlInputControl Name Example </title>
<script runat="server">

      void Page_Load(Object sender, EventArgs e)
      {

         // Create a data source.
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("Value", typeof(string)));
   
         for (int i = 0; i < 3; i++) 
         {
            dr = dt.NewRow();
  
            dr[0] = "Item " + i.ToString();
 
            dt.Rows.Add(dr);
         }
 
         // Bind the data source to the Repeater control.
         Repeater1.DataSource = new DataView(dt);
         Repeater1.DataBind();

      }

      void AddButton_Click(Object sender, EventArgs e)
      {
      
         Message.Text = "The name of the HtmlInputControl clicked is " + 
                        ((HtmlInputControl)sender).Name;

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlInputControl Name Example </h3>

      <asp:Repeater id="Repeater1"
           runat="server">

         <ItemTemplate>
            
            <input type="submit"
                   name="AddButton"
                   value='<%# DataBinder.Eval(Container.DataItem, "Value") %>'
                   onserverclick="AddButton_Click"
                   runat="server"/>

         </ItemTemplate>


      </asp:Repeater>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

   </form>

</body>

</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlInputControl Name Example </title>
<script runat="server">

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Create a data source.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         dt.Columns.Add(new DataColumn("Value", GetType(String)))

         Dim i As Integer   

         For i = 0 to 2

            dr = dt.NewRow()
  
            dr(0) = "Item " + i.ToString()
 
            dt.Rows.Add(dr)
         
         Next i
 
         ' Bind the data source to the Repeater control.
         Repeater1.DataSource = New DataView(dt)
         Repeater1.DataBind()

      End Sub

      Sub AddButton_Click(sender As Object, e As EventArgs)
      
         Message.Text = "The name of the HtmlInputControl clicked is " & _ 
                        CType(sender, HtmlInputControl).Name

      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlInputControl Name Example </h3>

      <asp:Repeater id="Repeater1"
           runat="server">

         <ItemTemplate>
            
            <input type="submit"
                   name="AddButton"
                   value='<%# DataBinder.Eval(Container.DataItem, "Value") %>'
                   onserverclick="AddButton_Click"
                   runat="server"/>

         </ItemTemplate>


      </asp:Repeater>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

   </form>

</body>

</html>

備註

Name使用 屬性來判斷 的唯一 HtmlInputControl 識別碼名稱。 在此實作中,存取 get 子會傳回 屬性的值 Control.UniqueID 。 不過,存取 set 子不會將值指派給這個屬性。

注意

存取 set 子不會將值指派給這個屬性, Name 因為屬性必須具有與屬性相同的值 Control.UniqueID ,大部分控制項才能正常運作。

如有需要,繼承自 類別的 HtmlInputControl 類別可以覆寫此實作。

適用於

另請參閱