Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 367, trackbacks - 0

My Links

News

Archives

Image Galleries

Limit the Number to be Selected in the ListBox

This example shows on how are we going 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;
}
}

}

Print | posted on Tuesday, February 17, 2009 3:19 PM |

Feedback

Gravatar

# re: Limit the Number to be Selected in the ListBox

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
11/24/2009 2:45 AM | sanjeev
Gravatar

# re: Limit the Number to be Selected in the ListBox

You would probably needs to use ViewState Management. Store the selected index in a ViewState and reference the value from there instead.

PS: Is there any reason why you wanted to disable the JavaScript on the page?
11/24/2009 1:38 PM | Vinz
Gravatar

# re: Limit the Number to be Selected in the ListBox

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
11/24/2009 2:54 PM | Vinz
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: