Hi Waghmare,
Check below code.
HTML
<asp:ScriptManager runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="Up1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstFruits" runat="server" SelectionMode="Multiple" name="fruits" onchange="return Validate()">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:ListBox>
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" OnClientClick="return Validate()" />
<br />
<span id="lblMessage" style="display: none; color: red;">Please select at least one fruit.</span>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" />
<script type="text/javascript" src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"></script>
<script type="text/javascript">
$(function () {
SetMultiSelect();
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
function endRequestHandle(sender, Args) {
SetMultiSelect();
};
});
function SetMultiSelect() {
$('[id*=lstFruits]').multiselect({
includeSelectAllOption: false
});
}
function Validate() {
$("#lblMessage").hide();
if ($("#lstFruits option:selected").length > 0) {
return true;
} else {
$("#lblMessage").show();
return false;
}
}
</script>
Code
C#
protected void Submit(object sender, EventArgs e)
{
string message = "";
foreach (ListItem item in lstFruits.Items)
{
if (item.Selected)
{
message += item.Text + " " + item.Value + "\\n";
}
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "<script>alert('Selected Fruits are\\n" + message + "')</script>", false);
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = ""
For Each item As ListItem In lstFruits.Items
If item.Selected Then
message += item.Text & " " + item.Value & "\n"
End If
Next
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "alert", "<script>alert('Selected Fruits are\n" & message & "')</script>", False)
End Sub