HtmlTextArea.Name 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 HtmlTextArea 控制項的唯一識別項名稱。
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 屬性來選取頁面上的 HtmlTextArea 控制項,並設定其 Value 屬性。
<%@ 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">
<script runat="server" >
void Page_Load(Object sender, EventArgs e)
{
// Bind a data source to the Repeater control.
Repeater1.DataSource = CreateRepeaterSource();
Repeater1.DataBind();
}
void Item_Bound(Object sender, RepeaterItemEventArgs e)
{
// The ItemDataBound event is raised when data is bound to an
// item in the Repeater control. Items can include the Header,
// Footer, and so on. Use the following logic only if the item
// being bound is an Item or AlternatingItem.
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// The runtime automatically generates a unique identifier
// for each control embedded in a list control, such as the
// Repeater control. The Name property of the HtmlTextArea
// control contains this unique identifier and is commonly used to
// identify a specific control.
// Retrieve the HtmlTextArea control from the RepeaterItem.
HtmlTextArea area = (HtmlTextArea)e.Item.FindControl("TextArea1");
// Insert a custom message for the fourth HtmlTextArea control by
// looking for a Name property that contains the number 3.
if (area.Name.Contains("3"))
{
area.Value = "Hello World";
}
}
}
DataView CreateRepeaterSource()
{
// Create a DataTable that contains sample data for the
// Repeater control.
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Category", typeof(String)));
// Populate the DataTable with sample values.
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = "Category " + i.ToString();
dt.Rows.Add(dr);
}
// Create a DataView from the DataTable.
DataView dv = new DataView(dt);
return dv;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlTextArea Name Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HtmlTextArea Name Example</h3>
Notice that Category 3 has custom text. <br />
<asp:Repeater id="Repeater1"
onitemdatabound="Item_Bound"
runat="server">
<ItemTemplate>
<h4><%# DataBinder.Eval(Container.DataItem, "Category") %></h4>
Enter text:
<br />
<textarea rows="2" cols="20" id="TextArea1"
runat="server"/>
<br /><br />
<hr />
</ItemTemplate>
</asp:Repeater>
</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">
<script runat="server" >
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Bind a data source to the Repeater control.
Repeater1.DataSource = CreateRepeaterSource()
Repeater1.DataBind()
End Sub
Sub Item_Bound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
' The ItemDataBound event is raised when data is bound to an
' item in the Repeater control. Items can include the Header,
' Footer, and so on. Use the following logic only if the item
' being bound is an Item or AlternatingItem.
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
' The runtime automatically generates a unique identifier
' for each control embedded in a list control, such as the
' Repeater control. The Name property of the HtmlTextArea
' control contains this unique identifier and is commonly used to
' identify a specific control.
' Retrieve the HtmlTextArea control from the RepeaterItem.
Dim area As HtmlTextArea = _
CType(e.Item.FindControl("TextArea1"), HtmlTextArea)
' Insert a custom message for the fourth HtmlTextArea control by
' looking for a Name property that contains the number 3.
If area.Name.Contains("3") Then
area.Value = "Hello World"
End If
End If
End Sub
Function CreateRepeaterSource() As DataView
' Create a DataTable that contains sample data for the
' Repeater control.
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("Category", GetType(String)))
' Populate the DataTable with sample values.
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = "Category " & i.ToString()
dt.Rows.Add(dr)
Next i
' Create a DataView from the DataTable.
Dim dv As DataView = New DataView(dt)
Return dv
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlTextArea Name Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HtmlTextArea Name Example</h3>
Notice that Category 3 has custom text. <br />
<asp:Repeater id="Repeater1"
onitemdatabound="Item_Bound"
runat="server">
<ItemTemplate>
<h4><%# DataBinder.Eval(Container.DataItem, "Category") %></h4>
Enter text:
<br />
<textarea rows="2" cols="20" id="TextArea1"
runat="server"/>
<br /><br />
<hr />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
備註
Name使用 屬性來判斷控制項的唯一 HtmlTextArea 識別碼名稱。 在這個 屬性的實作中,存取子會 get
傳回 屬性的值 Control.UniqueID 。 不過,存取 set
子不會將值指派給這個屬性。
注意
存取 set
子不會將值指派給這個屬性, Name 因為 屬性的值必須與 控制項的 屬性 HtmlTextArea 具有相同的值 Control.UniqueID ,才能正常運作。
如有需要,繼承自 類別的 HtmlTextArea 類別可能會覆寫此實作。