vbを使用してasp.netのリストボックスからのアイテム選択の数を制限したい。私はjavascriptを使用してそれを試していました。しかし、機能していません。助けが必要
<asp:ListBox ID="lbprefferedlocation" runat="server" DataSourceID="locations"
DataTextField="City_Name" OnSelectedIndexChanged="chkCount(this)" DataValueField="City_Name" SelectionMode="Multiple"></asp:ListBox>
<asp:HiddenField ID="hiddenChkCount" runat="server" />
<asp:SqlDataSource ID="locations" runat="server"
ConnectionString="<%$ ConnectionStrings:JPConnString %>"
SelectCommand="SELECT [City_Name] FROM [State_Info]"></asp:SqlDataSource>
function chkCount(obj)
{
if(obj.checked==true)
{
if( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value >= 5 )
{
alert('You cannot select more than 5 items.');
obj.checked=false;
}
else
{
document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value ) + 1;
}
}
else
{
document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value = parseInt( document.getElementById( '<%=hiddenChkCount.ClientID %>' ).value) - 1;
}
}