выбор одного элемента
событие при выборе элемента
динамическое добавление и удаление одного элемента
динамическое удаление нескольких элементов
событие при выборе элемента
динамическое добавление и удаление одного элемента
динамическое удаление нескольких элементов
выбор одного элемента
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ SelectionMode="Single" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> </asp:ListBox> <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Clear" OnClick="Button2_Click" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ SelectionMode="Single" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> </asp:ListBox> <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="Clear" OnClick="Button2_Click" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; } protected void Button2_Click(object sender, EventArgs e) { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; } protected void Button2_Click(object sender, EventArgs e) { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } }
событие при выборе элемента
Добавить атрибут AutoPostBack="true"
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ AutoPostBack="true" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single" AutoPostBack="true" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> <asp:ListItem Value="clear">Clear</asp:ListItem> </asp:ListBox> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ AutoPostBack="true" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single" AutoPostBack="true" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> <asp:ListItem Value="clear">Clear</asp:ListItem> </asp:ListBox> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; if(ListBox1.SelectedItem.Value == "clear") { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; if(ListBox1.SelectedItem.Value == "clear") { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } } }
динамическое добавление и удаление одного элемента
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Label ID="Label1" runat="server" Text="Add Text"></asp:Label> <br /> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="Add Value"></asp:Label> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" /> <br /> <br /> <asp:Button ID="Button2" runat="server" Text="Remove" OnClick="Button2_Click" /> <br /> <br /> <br /> <asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> </asp:ListBox> <asp:Button ID="Button3" runat="server" Text="OK" OnClick="Button3_Click" /> <asp:Button ID="Button4" runat="server" Text="Clear" OnClick="Button4_Click" /> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> <asp:Label ID="Label4" runat="server" Text=""></asp:Label> <asp:Label ID="Label5" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Label ID="Label1" runat="server" Text="Add Text"></asp:Label> <br /> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="Add Value"></asp:Label> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" /> <br /> <br /> <asp:Button ID="Button2" runat="server" Text="Remove" OnClick="Button2_Click" /> <br /> <br /> <br /> <asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> </asp:ListBox> <asp:Button ID="Button3" runat="server" Text="OK" OnClick="Button3_Click" /> <asp:Button ID="Button4" runat="server" Text="Clear" OnClick="Button4_Click" /> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> <asp:Label ID="Label4" runat="server" Text=""></asp:Label> <asp:Label ID="Label5" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //Add protected void Button1_Click(object sender, EventArgs e) { ListItem item = new ListItem(); item.Text = TextBox1.Text; item.Value = TextBox2.Text; ListBox1.Items.Add(item); } //Remove protected void Button2_Click(object sender, EventArgs e) { ListItem item = ListBox1.SelectedItem; ListBox1.Items.Remove(item); } //OK protected void Button3_Click(object sender, EventArgs e) { Label3.Text = ListBox1.SelectedIndex.ToString(); Label4.Text = ListBox1.SelectedItem.Text; Label5.Text = ListBox1.SelectedItem.Value; } //Clear protected void Button4_Click(object sender, EventArgs e) { Label3.Text = string.Empty; Label4.Text = string.Empty; Label5.Text = string.Empty; ListBox1.ClearSelection(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //Add protected void Button1_Click(object sender, EventArgs e) { ListItem item = new ListItem(); item.Text = TextBox1.Text; item.Value = TextBox2.Text; ListBox1.Items.Add(item); } //Remove protected void Button2_Click(object sender, EventArgs e) { ListItem item = ListBox1.SelectedItem; ListBox1.Items.Remove(item); } //OK protected void Button3_Click(object sender, EventArgs e) { Label3.Text = ListBox1.SelectedIndex.ToString(); Label4.Text = ListBox1.SelectedItem.Text; Label5.Text = ListBox1.SelectedItem.Value; } //Clear protected void Button4_Click(object sender, EventArgs e) { Label3.Text = string.Empty; Label4.Text = string.Empty; Label5.Text = string.Empty; ListBox1.ClearSelection(); } }
динамическое удаление нескольких элементов
Добавить атрибут SelectionMode="Multiple"
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ SelectionMode="Multiple" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> </asp:ListBox> <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click"/> <asp:Button ID="Button2" runat="server" Text="Clear" OnClick="Button2_Click"/> <asp:Button ID="Button3" runat="server" Text="Remove" OnClick="Button3_Click"/> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta charset="utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%-- ДОБАВИТЬ АТРИБУТ SelectionMode="Multiple" --%> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"> <asp:ListItem Value="#FF0000">Red</asp:ListItem> <asp:ListItem Value="#00FF00">Green</asp:ListItem> <asp:ListItem Value="#0000FF">Blue</asp:ListItem> </asp:ListBox> <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click"/> <asp:Button ID="Button2" runat="server" Text="Clear" OnClick="Button2_Click"/> <asp:Button ID="Button3" runat="server" Text="Remove" OnClick="Button3_Click"/> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //OK protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; } //Clear protected void Button2_Click(object sender, EventArgs e) { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } //Remove protected void Button3_Click(object sender, EventArgs e) { for(int i = ListBox1.Items.Count - 1; i >= 0; i--) { ListItem item = ListBox1.Items[i]; if(item.Selected) { ListBox1.Items.Remove(item); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //OK protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ListBox1.SelectedIndex.ToString(); Label2.Text = ListBox1.SelectedItem.Text; Label3.Text = ListBox1.SelectedItem.Value; } //Clear protected void Button2_Click(object sender, EventArgs e) { Label1.Text = string.Empty; Label2.Text = string.Empty; Label3.Text = string.Empty; ListBox1.ClearSelection(); } //Remove protected void Button3_Click(object sender, EventArgs e) { for(int i = ListBox1.Items.Count - 1; i >= 0; i--) { ListItem item = ListBox1.Items[i]; if(item.Selected) { ListBox1.Items.Remove(item); } } } }