ListItem.Value Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá nebo nastaví hodnotu přidruženou k objektu ListItem.
public:
property System::String ^ Value { System::String ^ get(); void set(System::String ^ value); };
public string Value { get; set; }
member this.Value : string with get, set
Public Property Value As String
Hodnota vlastnosti
Hodnota přidružená k ListItem. Výchozí formát je Empty.
Příklady
Poznámka
Následující ukázka kódu používá model s jedním souborem kódu a nemusí správně fungovat, pokud je zkopírován přímo do souboru s kódem na pozadí. Tento vzorek kódu musí být zkopírován do prázdného textového souboru s příponou .aspx. Další informace o modelu kódu Web Forms najdete v tématu ASP.NET Web Forms model kódu stránky.
<!-- 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>
Poznámky
Value Pomocí vlastnosti určete nebo určete hodnotu přidruženou k objektu ListItem.
Poznámka
Value Pokud vlastnost obsahuje null
, get
vrátí přistupný objekt hodnotu Text vlastnosti . Text Pokud vlastnost zase obsahuje null
, String.Empty vrátí se.
Hodnota této vlastnosti, je-li nastavena, lze automaticky uložit do souboru prostředků pomocí nástroje návrháře. Další informace najdete v tématech LocalizableAttribute a Globalizace a lokalizace.
Platí pro
Viz také
- Text
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- CheckBoxList
- Přehled ovládacího prvku webového serveru ListBox
- Přehled ovládacích prvků webového serveru RadioButton a RadioButtonList
- Přehled ovládacích prvků webového serveru CheckBox a CheckBoxList
- Přehled ovládacího prvku webového serveru BulletedList
- Přehled ovládacího prvku webového serveru DropDownList
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro