Tuesday, September 22, 2015

ASP.NET CheckBoxList, Select or Deselect all list items

Suggested Videos
Part 17 - Data bind dropdownlist with data from the database
Part 21 - Retrieving selected item text, value and index of the dropdownlist
Part 23 - Asp.net checkboxlist control

In this video we will learn about
1. Selecting a specific ListItem with-in a CheckBoxList control using SelectedValue and SelectedIndex properties
2. Selecting or De-Selecting all ListItems of a CheckBoxList control

To have a ListItem pre-selected, when the page renders, we can do that in the HTML by setting the Selected property to True as shown below.
<asp:ListItem Text="Diploma" Selected="True" Value="1"></asp:ListItem>



This can be programmatically done, using the SelectedValue property as shown below.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckBoxList1.SelectedValue = "1";
    }
}

SelectedIndex property can also be used.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckBoxList1.SelectedIndex = 2;
    }
}



Copy and paste the following HTML and Code in the ASPX and the code behind page
<asp:Button ID="buttonSelectAll" runat="server" Text="Select All" 
    onclick="buttonSelectAll_Click" /> 
&nbsp; 
<asp:Button ID="buttonDeselectAll" runat="server" Text="De-Select All" 
    onclick="buttonDeselectAll_Click" />
<br /><br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
    RepeatDirection="Horizontal">
    <asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
    <asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
    <asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
    <asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
</asp:CheckBoxList>

protected void buttonSelectAll_Click(object sender, EventArgs e)
{
    foreach (ListItem li in CheckBoxList1.Items)
    {
        li.Selected = true;
    }
}

protected void buttonDeselectAll_Click(object sender, EventArgs e)
{
    foreach (ListItem li in CheckBoxList1.Items)
    {
        li.Selected = false;
    }
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment