More

    ListBox Control in ASP.Net: The Most Innovative Things

    ListBox control is an asp.net web server control. The ListBox control is used to store (store) multiple items and allows the user to select more than one item from the ListBox control. ListBox control is similar to dropdownlist control.

    Dropdownlist control allows the user to select only one item at a time, on the other hand, ListBox control allows the user to select multiple items at the same time. So we can also say that ListBox is a multi row selection box control.

    A ListBox control uses the SelectionMode property to change the mode of a section from single to multiple. The selectionMode of the default listbox control is single If you want to select multiple items from the listbox, change the SelectionMode property to multiple.

    ListBox Control example using C # in ASP.Net

    How to use ListBox control in Asp.net C #?

    Some important properties of ListBox control

    Items: Items property is used to add items to listbox control. This indicates the collection of items in the listbox.

    When adding a new object to a ListBox control, two main things to be noted, each item of the ListBox control has two attributes, such as Text and Value. When adding a new item to the listbox we need to give two things text and a value for each item.

    Text: Text property displays the text in the listbox.

    ValueThe: value property is an invisible value, but we can get the value while programming. Each item has a text and value.

    To add new items to the listbox control, go to the lsitbox item property and show the items like the screen below.

    asp net list box items control property

    How to add new item to listbox control in ASP.Net

    If you click on the items property of the listbox, you have the screen below to add the item. Click the Add button to add the item to the listbox.

    Add some item with the item’s text and value and use the add button to add items to the listbox control.

    listbox control in asp.net

    Some other important properties of listbox are:

    • ListBox1.Items.Count = Return the total number of items in the listbox.
    • ListBox1.Items.Add (“ItemName”) = Add new item to listbox control.
    • ListBox1.Items.Insert (int index, “ItemName”) = insert a new item in a particular location in the listbox control.
    • ListBox1.Items.Remove (“ItemName”) = Remove item from listbox control.
    • ListBox1.Items.RemoveAt (int index) = Remove the item from any desired index of the listbox.
    • ListBox1.Items.Clear () = clear all item from listbox control.
    • ListBox1.SelectedItem.Text = Returns the text value of the selected item of the listbox.
    • ListBox1.SelectedValue = Returns the Value property of the selected item of the listbox.
    • ListBox1.SelectedIndex = Returns the index of the selected item of the list. (index always starts at 0).
    • Here, the code for bind listbox control is provided from the database.
    • ListBox1.DataSource = DataTable or DataSet
    • ListBox1.DataTextField = This binds the text to the listbox. (It appears in the listbox)
    • ListBox1.DataValueField = This binds the value to the listbox. (It remains invisible in the listbox)

    Let’s take an example to understand all the propertise of listbox control in asp.net c #.

    Who discovered black hole? : A Look Into the Future

    Design the asp.net web page given below:

    Here is the HTML code to design the asp.net web page: –

    <body>
      <form id="form1" runat="server">
      <div>
      
        <table align="center" class="style1" style="border: thin solid #008080">
          <tr>
            <td class="style2" 
              style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">
              ListBox Control in ASP.Net</td>
          </tr>
          <tr>
            <td style="text-align: center">
     
               
              <asp:ListBox ID="ListBox1" runat="server" Height="121px" Width="98px">
                <asp:ListItem Value="1">ABC</asp:ListItem>
                <asp:ListItem Value="2">PQR</asp:ListItem>
                <asp:ListItem Value="3">MNO</asp:ListItem>
                <asp:ListItem Value="4">XYZ</asp:ListItem>
              </asp:ListBox>
            </td>
          </tr>
          <tr>
            <td class="style3">
              <asp:Button ID="btncount" runat="server" CssClass="style4" 
                onclick="btncount_Click" Text="Count" Width="86px" />
     
              <asp:Button ID="btnselectedtext" runat="server" CssClass="style4" 
                onclick="btnselectedtext_Click" Text="Selected Text" />
     
              <asp:Button ID="btnselectedvalue" runat="server" CssClass="style4" 
                onclick="btnselectedvalue_Click" Text="Selected Value" />
     <asp:Button ID="btnselectedIndex" runat="server" CssClass="style4" 
                onclick="btnselectedIndex_Click" Text="Index" />
              <br />
     <asp:Button ID="btnclear" runat="server" CssClass="style4" onclick="btnclear_Click" 
                Text="Clear()" Width="83px" />
     
              <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" 
                style="font-weight: 700" Text="ADD" Width="67px" />
      
              <asp:Button ID="btnremove" runat="server" CssClass="style4" 
                onclick="btnremove_Click" Text="Remove" />
            </td>
          </tr>
          <tr>
            <td style="text-align: center">
              <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" 
                ForeColor="#CC0000"></asp:Label>
            </td>
          </tr>
        </table>
      
      </div>
      </form>
    </body>

    Here, we have 7 button control to understand all the propertise of listbox control.

    ListBox1.Items.Count

    The example below shows the total number of items in the listbox control: –

    protected void btncount_Click(object sender, EventArgs e)
     {
      Label1.Text = "The Count = "+ListBox1.Items.Count.ToString();
     }

    listbox control in asp.net

    ListBox1.SelectedItem.Text

    Example: How to get the selected item from listbox control in asp.net.

    protected void btnselectedtext_Click(object sender, EventArgs e)
     {
      Label1.Text = "Text = "+ListBox1.SelectedItem.Text;
     }

    listbox control in asp.net

    ListBox1.SelectedValue

    Example: How to get the selected value from listbox control in asp.net.

    protected void btnselectedvalue_Click(object sender, EventArgs e)
    {
      Label1.Text = "Value = " +ListBox1.SelectedValue;
    }

    listbox control in asp.net

    ListBox1.SelectedIndex

    Get the selected value from listbox control in asp.net c #:

    protected void btnselectedIndex_Click(object sender, EventArgs e)
     {
      Label1.Text = "Index = " +ListBox1.SelectedIndex.ToString();
     }

    ListBox1.Items.Clear()

    Example: clear all items of listbox control.

    protected void btnclear_Click(object sender, EventArgs e)
     {
       ListBox1.Items.Clear();
       Label1.Text = "ListBox Cleared";
     }

    How to clear all item in listbox in asp.net c #.

    protected void btnadd_Click(object sender, EventArgs e)
     {
       ListBox1.Items.Add("Meera");
       Label1.Text = "Item Added";
     }

    ListBox1.Items.Remove (“text”)

    Example: how to remove items from listbox control.

    protected void btnremove_Click(object sender, EventArgs e)
     {
       ListBox1.Items.Remove("Meera");
       Label1.Text = "Item Removed";
     }

    How to remove items from listbox in asp.net c #.

    ListBox1.Items.Insert (int index, “text”);

    Example: Add a new item to a specific location in the listbox control. The index indicates the location of the newly added item in the listbox control.

    protected void btnadd_Click(object sender, EventArgs e)
     {
       ListBox1.Items.Insert(2,"Meera");
     }

    ListBox1.Items.RemoveAt (int index)

    Example: removing an item from a specified location of the listbox control. The index starts at 0.

    protected void btnremove_Click(object sender, EventArgs e)
     {
       ListBox1.Items.RemoveAt(2);
       Label1.Text = "Item Removed";
     }

    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox