Archive

Archive for August, 2012

Single selection Checkboxlist in Asp.net with the help of JQUERY

August 2, 2012 5 comments

INTRODUCTION
As we know CheckBoxList asp.net server control that comes under “System.Web.UI.WebControls” namespace allow multi selection . In this Small post I will show you how to make single selection in CheckBoxList asp.net server control using J Query.

HTML for Control
When Binding dynamically from database

<asp:CheckBoxList ID="chkTest" runat="server"
RepeatDirection="Vertical"
RepeatColumns="1">
</asp:CheckBoxList>

When it is static

<asp:CheckBoxList ID="chkTest" runat="server"
RepeatLayout=Flow  RepeatDirection="Horizontal" Width="99%">
<asp:ListItem Text="Test1" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Test2" Value="2"></asp:ListItem>
<asp:ListItem Text="Test3" Value="3"></asp:ListItem>
<asp:ListItem Text="Test4" Value="4"></asp:ListItem>
</asp:CheckBoxList>

JQuery for this

<script type="text/javascript">
$(document).ready(function() {
var checkboxes = $('#<%=chkTest.ClientID %>').find('input:checkbox');
checkboxes.click(function() {
var selectedIndex = checkboxes.index($(this));

var items = $('#<% = chkTest.ClientID %> input:checkbox');
for (i = 0; i < items.length; i++) {
if (i == selectedIndex)
items[i].checked = true;
else
items[i].checked = false;
}
});
});
</script>

I hope . you will find it helpfull.

Categories: ASP.NET, JQuery Tags: ,
%d bloggers like this: