Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows how to limit the number of Listitems to be selected in the ListBox control.

ASPX

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function GetCurrentItemsSelected(obj)
{
var o = obj;
document.getElementById('<%= HiddenField1.ClientID %>').value = o;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="109px" SelectionMode="Multiple"
                           OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="141px">
<asp:ListItem onclick ="GetCurrentItemsSelected('0');">A</asp:ListItem>
<asp:ListItem onclick ="GetCurrentItemsSelected('1');">B</asp:ListItem>
<asp:ListItem onclick ="GetCurrentItemsSelected('2');">C</asp:ListItem>
<asp:ListItem onclick ="GetCurrentItemsSelected('3');">D</asp:ListItem>
<asp:ListItem onclick ="GetCurrentItemsSelected('4');">E</asp:ListItem>
<asp:ListItem onclick ="GetCurrentItemsSelected('5');">F</asp:ListItem>
</asp:ListBox>
<asp:HiddenField ID="HiddenField1" runat="server" />
</form>
</body>
</html>

 
 

RELEVANT CODES:

 

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ListBox1.ClearSelection();
}
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int count = 0;
int limit = 0;
int lastSelectedIndex = 0;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (ViewState["CountLimit"] != null)
{
count++;
ViewState["CountLimit"] = count;
limit = (int)ViewState["CountLimit"];
}
else
{
count++;
ViewState["CountLimit"] = count;
}
if (limit > 3) // Selected Item Limit is up to 3
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowBox", "alert('You are only allowed to Select 3 items!');", true);
//Get the last selected items selected when the items selected exceeds in 3
lastSelectedIndex = int.Parse(HiddenField1.Value.ToString());
//Unselect the 4th items selected
ListBox1.Items[lastSelectedIndex].Selected = false;
}
}

}
 
Technorati Tags: ,
Posted on Tuesday, February 17, 2009 3:19 PM ASP.NET , Tips&Tricks | Back to top


Comments on this post: Limit the Number to be Selected in the ListBox

# re: Limit the Number to be Selected in the ListBox
Requesting Gravatar...
This code wont run if javascript is not enabled in client browser. So basically there should be some way to get the last selected value in the code behind not from the java script. If you u find anything pls send me too. thanks
Left by sanjeev on Nov 24, 2009 2:45 AM

# re: Limit the Number to be Selected in the ListBox
Requesting Gravatar...
Here's the server side solution:

http://geekswithblogs.net/dotNETvinz/archive/2009/11/24/limit-the-number-to-be-selected-in-the-listbox-control.aspx
Left by Vinz on Nov 24, 2009 2:54 PM

# re: Limit the Number to be Selected in the ListBox
Requesting Gravatar...
I don't know may be I am mistaken but I try this code but instead of last selected item first selected item is to be clear.. So i try this one without any view state..

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int count = 0;

int limit = 0;

int lastSelectedIndex = 0;

int currentSelected = 0;



for (int i = 0; i < ListBox1.Items.Count; i++)
{
lastSelectedIndex = i;
if (ListBox1.Items[i].Selected)
{


count++;


if (count > 3) // Selected Item Limit is up to 3
{

Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowBox", "alert('You are only allowed to Select 3 items!');", true);

ListBox1.Items[lastSelectedIndex].Selected = false;

}

}

}
}
Left by Jaynesh on Jul 22, 2010 10:50 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net