Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday 19 March 2015

We can select multiple values from listbox. Sometime it is needed to select multiple values from listbox while development. I thi s article I will explain how to get multiple values from listbox and display in Label control.

Design the webform as given below:
  <fieldset style="width:450px">
    <legend> Get Multiple values from listBox</legend> 
    Country :
<asp:ListBox ID="lstcountry" runat="server" Width="100px" SelectionMode="Multiple">
            <asp:ListItem>India</asp:ListItem>
            <asp:ListItem>China</asp:ListItem>
            <asp:ListItem>Cuba</asp:ListItem>
            <asp:ListItem>Other</asp:ListItem>
        </asp:ListBox>
        <br /> 
        <br />
<asp:Button
            ID="btnsubmit" runat="server" Text="Get Value" onclick="btnsubmit_Click" /> 
        <asp:Label ID="lblmessage" runat="server"></asp:Label> 
    </fieldset>

ASP.NET Code (C#)
On button click write the below given code (C#):
  protected void btnsubmit_Click(object sender, EventArgs e)
    {
        foreach (int i in lstcountry.GetSelectedIndices())
        {
            lblmessage.Text += lstcountry.Items[i].Value+",";
        }

    }
VB.NET section:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handlesbtnsubmit.Click
        For Each i As Integer In lstcountry.GetSelectedIndices()
            lblmessage.Text += lstcountry.Items(i).Value + ","
        Next

    End Sub
Categories: , ,

0 comments:

Post a Comment