Get selected item from ListBox (C#) : ListBox « Asp Control « ASP.Net






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>

           
       








Related examples in the same category

1.Set static value for asp:listbox (VB.net)
2.asp:listbox: On selected index changed
3.Get selected index, text and value from asp:listbox (VB.net)
4.Change background color for asp:listbox (VB.net)
5.Allowing Multiple Selections in a ListBox Control (VB.net)
6.Get selected index from asp:listbox (VB.net)
7.Add ListItem to ListBox
8.Using the ListBox to select list items.