Quantcast
Channel: AuthorCode » ListBox control
Viewing all articles
Browse latest Browse all 9

ListBox control in asp.net

$
0
0

ListBox control contains list of options. We can create ListBox with single option select and multiple option select functionality. We can add a list of option or items in these ways:

At the time declaration:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3.     <title>AutoPostBack property of ListBox</title>
  4. </head>
  5. <body style="height: 410px; width: 768px">
  6.     <form runat="server">
  7.     <asp:ListBox ID="ListBox1" runat="server">
  8.     <asp:ListItem Text="USA" Value="1" />
  9.     <asp:ListItem Text="India" Value="2" />
  10.     <asp:ListItem Text="Japan" Value="3" />
  11.     </asp:ListBox>
  12.    </form>
  13. </body>
  14. </html>

Add items directly to the Item collection:

  1. <script runat="server">
  2.     Sub Page_Load()
  3.         ListBox1.Items.Add("USA")
  4.         ListBox1.Items.Add("India")
  5.         ListBox1.Items.Add("Japan")
  6.         ListBox1.Items.Add("UK")
  7.     End Sub
  8. </script>
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head id="Head1" runat="server">
  11.     <title>Add items directly to the Item collection</title>
  12. </head>
  13. <body style="height: 410px; width: 768px">
  14.     <form runat="server">
  15.     <asp:ListBox ID="ListBox1" runat="server">
  16.     </asp:ListBox>
  17.    </form>
  18. </body>
  19. </html>

And in the last if you want to bind control to a DataSource, for example:
In the following example first we create a array list and then we assign this array list to datasource property of ListBox control:

  1. <script runat="server">
  2.     Sub Page_Load()
  3.         Dim arrList As New ArrayList
  4.         arrList.Add("USA")
  5.         arrList.Add("India")
  6.         arrList.Add("Japan")
  7.         ListBox1.DataSource = arrList
  8.         ListBox1.DataBind()
  9.     End Sub
  10. </script>
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head id="Head1" runat="server">
  13.     <title>Add items directly to the Item collection</title>
  14. </head>
  15. <body style="height: 410px; width: 768px">
  16.     <form runat="server">
  17.     <asp:ListBox ID="ListBox1" runat="server">
  18.     </asp:ListBox>
  19.    </form>
  20. </body>
  21. </html>

Viewing all articles
Browse latest Browse all 9

Trending Articles