ListItem 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ListItem 類別的新執行個體。
多載
ListItem() |
初始化 ListItem 類別的新執行個體。 |
ListItem(String) |
使用指定的文字資料,初始化 ListItem 類別的新執行個體。 |
ListItem(String, String) |
使用指定的文字和值資料,初始化 ListItem 類別的新執行個體。 |
ListItem(String, String, Boolean) |
使用指定的文字、值和啟用資料,初始化 ListItem 類別的新執行個體。 |
ListItem()
初始化 ListItem 類別的新執行個體。
public:
ListItem();
public ListItem ();
Public Sub New ()
範例
注意
下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型。
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that value. When the new ListBoxItem is created, it receives
the Value property of the selected item as its Text property, and the Text property
of the selected item as its value property. -->
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
void AddBtn_Click(Object Sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
{
if (ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) == null)
{
ListItem Item = new ListItem();
// Text and Value are swapped.
Item.Text = ListBox1.SelectedItem.Value;
Item.Value = ListBox1.SelectedItem.Text;
ListBox2.Items.Add(Item);
}
}
}
void DelBtn_Click(Object Sender, EventArgs e)
{
if (ListBox2.SelectedIndex > -1)
{
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that value. When the new ListBoxItem is created, it receives
the Value property of the selected item as its Text property, and the Text property
of the selected item as its value property. -->
<!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>ListItem Example</title>
<script language="VB" runat="server">
Sub AddBtn_Click(Sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > -1 Then
If ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) is Nothing Then
Dim Item As ListItem = new ListItem()
'Text and Value are swapped
Item.Text = ListBox1.SelectedItem.Value
Item.Value = ListBox1.SelectedItem.Text
ListBox2.Items.Add(Item)
End If
End If
End Sub
Sub DelBtn_Click(Sender As Object, e As EventArgs)
If ListBox2.SelectedIndex > -1 Then
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
備註
使用此建構函式,使用預設值來建立和初始化 類別的新實例 ListItem 。
下表顯示 實例 ListItem 的初始屬性值。
屬性 | 初始值 |
---|---|
Text |
null |
Value |
null |
Enabled |
true |
另請參閱
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- CheckBoxList
- ListBox Web 服務器控制項概觀
- RadioButton 和 RadioButtonList Web 服務器控制項概觀
- BulletedList Web 服務器控制項概觀
- DropDownList Web 服務器控制項概觀
適用於
ListItem(String)
使用指定的文字資料,初始化 ListItem 類別的新執行個體。
public:
ListItem(System::String ^ text);
public ListItem (string text);
new System.Web.UI.WebControls.ListItem : string -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String)
參數
範例
注意
下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型。
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that text. -->
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
void AddBtn_Click(Object Sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
{
if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null)
{
ListItem Item = new ListItem(ListBox1.SelectedItem.Text);
Item.Value = ListBox1.SelectedItem.Value;
ListBox2.Items.Add(Item);
}
}
}
void DelBtn_Click(Object Sender, EventArgs e)
{
if (ListBox2.SelectedIndex > -1)
{
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that text. -->
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
Sub AddBtn_Click(Sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > -1 Then
If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
Dim Item As ListItem = new ListItem(ListBox1.SelectedItem.Text)
Item.Value = ListBox1.SelectedItem.Value
ListBox2.Items.Add(Item)
End If
End If
End Sub
Sub DelBtn_Click(Sender As Object, e As EventArgs)
If ListBox2.SelectedIndex > -1 Then
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
備註
使用此建構函式,使用指定的文字來建立和初始化 類別的新實例 ListItem 。
下表顯示 實例 ListItem 的初始屬性值。
屬性 | 初始值 |
---|---|
Text |
text 參數的值。 |
Value |
null |
Enabled |
true |
另請參閱
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- CheckBoxList
- ListBox Web 服務器控制項概觀
- RadioButton 和 RadioButtonList Web 服務器控制項概觀
- BulletedList Web 服務器控制項概觀
- DropDownList Web 服務器控制項概觀
適用於
ListItem(String, String)
使用指定的文字和值資料,初始化 ListItem 類別的新執行個體。
public:
ListItem(System::String ^ text, System::String ^ value);
public ListItem (string text, string value);
new System.Web.UI.WebControls.ListItem : string * string -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String, value As String)
參數
範例
注意
下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型。
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that text. -->
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
void AddBtn_Click(Object Sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
{
if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null)
{
ListItem Item = new ListItem(ListBox1.SelectedItem.Text,
ListBox1.SelectedItem.Value);
ListBox2.Items.Add(Item);
}
}
}
void DelBtn_Click(Object Sender, EventArgs e)
{
if (ListBox2.SelectedIndex > -1)
{
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already
contain an item with that text. -->
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
Sub AddBtn_Click(Sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > -1 Then
If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
Dim Item As ListItem = new ListItem(ListBox1.SelectedItem.Text, _
ListBox1.SelectedItem.Value)
ListBox2.Items.Add(Item)
End If
End If
End Sub
Sub DelBtn_Click(Sender As Object, e As EventArgs)
If ListBox2.SelectedIndex > -1 Then
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
備註
使用此建構函式,使用指定的文字和值來建立和初始化 類別的新實例 ListItem 。
下表顯示 實例 ListItem 的初始屬性值。
屬性 | 初始值 |
---|---|
Text |
text 參數的值。 |
Value |
value 參數的值。 |
Enabled |
true |
另請參閱
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- CheckBoxList
- ListBox Web 服務器控制項概觀
- RadioButton 和 RadioButtonList Web 服務器控制項概觀
- BulletedList Web 服務器控制項概觀
- DropDownList Web 服務器控制項概觀
適用於
ListItem(String, String, Boolean)
使用指定的文字、值和啟用資料,初始化 ListItem 類別的新執行個體。
public:
ListItem(System::String ^ text, System::String ^ value, bool enabled);
public ListItem (string text, string value, bool enabled);
new System.Web.UI.WebControls.ListItem : string * string * bool -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String, value As String, enabled As Boolean)
參數
範例
下列範例示範如何將專案新增至 控制項,並從控制項中移除專案 ListBox 。 在控制項中 ListBox1
選取專案時,如果 ListBox2
控制項尚未包含具有該文字的專案,則可以建立具有相同值的新 ListItem 控制項並新增至 ListBox2
控制項。 在此範例中,會呼叫建構函式,並將 enabled
設定為 true
。 如果呼叫時 enabled
已設定為 false
,則新 ListItem 控制項不會出現在 控制項中 ListBox 。
注意
下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
void AddBtn_Click(Object Sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
{
if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null)
{
ListItem Item = new ListItem(ListBox1.SelectedItem.Text,
ListBox1.SelectedItem.Value, true);
ListBox2.Items.Add(Item);
}
}
}
void DelBtn_Click(Object Sender, EventArgs e)
{
if (ListBox2.SelectedIndex > -1)
{
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>ListItem Example</title>
<script runat="server">
Sub AddBtn_Click(Sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > -1 Then
If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
Dim Item As ListItem = New ListItem(ListBox1.SelectedItem.Text, _
ListBox1.SelectedItem.Value, True)
ListBox2.Items.Add(Item)
End If
End If
End Sub
Sub DelBtn_Click(Sender As Object, e As EventArgs)
If ListBox2.SelectedIndex > -1 Then
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
</script>
</head>
<body>
<h3>ListItem Example</h3>
<form id="form1" runat="server">
<table>
<tr><td>
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem Value="Value 1">Item 1</asp:ListItem>
<asp:ListItem Value="Value 2">Item 2</asp:ListItem>
<asp:ListItem Value="Value 3">Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
<asp:ListItem Value="Value 6">Item 6</asp:ListItem>
</asp:ListBox>
</td><td>
<asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
<asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
</td><td>
<asp:ListBox id="ListBox2" Width="100px" runat="server"/>
</td></tr>
</table>
</form>
</body>
</html>
備註
下表顯示 實例 ListItem 的初始屬性值。
屬性 | 初始值 |
---|---|
Text |
text 參數的值。 |
Value |
value 參數的值。 |
Enabled |
enabled 參數的值。 |
另請參閱
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- CheckBoxList
- ListBox Web 服務器控制項概觀
- RadioButton 和 RadioButtonList Web 服務器控制項概觀
- BulletedList Web 服務器控制項概觀
- DropDownList Web 服務器控制項概觀
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應