.net - "Maximum-Count of Selected Items"-Validator for ListBox -
i have asked myself if there easy way check if listbox has maximum of 5 selected items. there must @ least 1 , @ 5 items selected.
do need customvalidator server-side validation?
many in advance...
you can customvalidator routine.
<asp:customvalidator id="listbox5itemsvalidator" runat="server" onservervalidate="listbox5itemsvalidator_servervalidate" clientvalidationfunction="listbox5itemsvalidator_clientvalidate" controltovalidate="mylistbox"> </asp:customvalidator>
server-side code:
protected void listbox5itemsvalidator_servervalidate( object source, servervalidateeventargs args) { int selectioncount = 0; foreach (listitem item in mylistbox.items) { if (item.selected) selectioncount++; } args.isvalid = (selectioncount >= 1 && selectioncount <= 5); }
client-side code:
function listbox5itemsvalidator_clientvalidate(sender, args) { var selectioncount = $('#<% =mylistbox.clientid %> option:selected').length; args.isvalid = (selectioncount >= 1 && selectioncount <= 5); };
replace mylistbox actual name of listbox want validate. if listbox contained inside of other container controls, may need little more work reference control both on server , client side. instance, if contained in formview control called formview1, use
listbox mylistbox = (listbox)formview1.findcontrol("mylistbox");
Comments
Post a Comment