Introduction:
In this article today I am going to explain how we can get multiple selected values from
Listbox in asp.net
Description:
In the previous article I have explained Format
theemails using StringBulider in asp.net, Disable
copy, cut, paste and drop options in asp.net textbox using Jquery, Explain
cookies in asp.net OR Explain cookies with example in asp.net and Display number of character left and count word in asp.net multiline textbox using Jquery plugin.
Design the webform as given below:
<fieldset style="width:450px">
<legend>Listbox
Example</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>
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:
Protected
Sub btnsubmit_Click(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
btnsubmit.Click
For Each i As Integer In
lstcountry.GetSelectedIndices()
lblmessage.Text +=
lstcountry.Items(i).Value + ","
Next
End Sub
Build and run the project.
Demo:
Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.
No comments:
Post a Comment