Mega Code Archive

 
Categories / ASP.Net / Asp Control
 

Get selected item from ListBox (C#)

<%@ Page Language="C#" %> <script runat="server">     void Page_Load()       {       string msgCitiesList = "";            if (Page.IsPostBack == true)              if (list1.Items[0].Selected == true)         {           msgCitiesList = msgCitiesList + list1.Items[0].Text + "<br />";         }              if (list1.Items[1].Selected)         {           msgCitiesList = msgCitiesList + list1.Items[1].Text + "<br/>";         }              if (list1.Items[2].Selected)         {           msgCitiesList = msgCitiesList + list1.Items[2].Text + "<br />";         }              if (msgCitiesList != "")         {            Message.Text = "You have selected: <br />" + msgCitiesList;         }         else         {            Message.Text = "";         }       } </script> <html> <head>     <title>List Box Example</title> </head> <body>     <asp:Label id="Message" runat="server"></asp:Label>     <br />     Which city do you wish to look at hotels for?<br />     <form runat="server">         <asp:listbox id="list1" runat="server" selectionmode="multiple">             <asp:listitem>A</asp:listitem>             <asp:listitem>B</asp:listitem>             <asp:listitem>C</asp:listitem>         </asp:listbox>         <br />         <input type="submit" value="Submit Query" />     </form> </body> </html>