0

I have 5 checkboxes in my webform.

i have button1 whose text is select all ....

i want when button1 text is select all then i clcik on button then all the checkboxes will be checked and button1 text will be unselect all .... if the button text is unselect all then all the checkboxes will be unchecked ...

how to do this using vb.net ?

6
  • You know, a (small) fragment of the .aspx would make answering a lot easier. Commented Mar 20, 2011 at 18:02
  • Are the Checkboxes all on a Panel? Commented Mar 20, 2011 at 18:02
  • ya ... i have the question mentioned above ... only Commented Mar 20, 2011 at 18:05
  • Ya all the checkboxes are inside panel 1 and panel1 is inside updatepanel and all the controsl are in usercontrol Commented Mar 20, 2011 at 18:06
  • @Sumit: You can Edit your question and add information. Commented Mar 20, 2011 at 18:07

2 Answers 2

3

Something like this should get it

ASPX:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Check All" />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Text="apple" Value="apple" />
            <asp:ListItem Text="banana" Value="banana" />
            <asp:ListItem Text="grapes" Value="grapes" />
            <asp:ListItem Text="kiwi" Value="kiwi" />
            <asp:ListItem Text="orange" Value="orange" />
        </asp:CheckBoxList>
    </ContentTemplate>
</asp:UpdatePanel>

VB.NET:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim toggle As Boolean = True
    If Button1.Text = "Check All" Then
        Button1.Text = "Uncheck All"
    Else
        Button1.Text = "Check All"
        toggle = False
    End If

    For Each li As ListItem In CheckBoxList1.Items
        li.Selected = toggle
    Next
End Sub

Alternatively you could do this easily client side jquery

something like

$('#Button1').click(function() {
    var toggle = true;
    if ($(this).val() === "Check All") {
        $(this).val("Uncheck All");
    } else {
        $(this).val("Check All");
        toggle = false;
    }
    $('.myCheckBoxes :checkbox').attr("checked", toggle);
});
Sign up to request clarification or add additional context in comments.

Comments

0

I'd recommend using jQuery for this. You would need something like the following in your ASPX file:

<script type="text/javascript">
    $(function() {
        $(".select-all-button").click(function() {
            var any = ($(".cb > input:checked").length > 0);
            $(".cb > input").attr("checked", !any);
        });
    });
</script>

<asp:Button ID="SelectAllButton" Text="Select All" CssClass="select-all-button" runat="server"/><br/><br/>

<asp:CheckBox ID="CheckBox1" CssClass="cb" Text="CheckBox 1" runat="Server" /><br/>
<asp:CheckBox ID="CheckBox2" CssClass="cb" Text="CheckBox 2" runat="Server" /><br/>
<asp:CheckBox ID="CheckBox3" CssClass="cb" Text="CheckBox 3" runat="Server" /><br/>
<asp:CheckBox ID="CheckBox4" CssClass="cb" Text="CheckBox 4" runat="Server" /><br/>
<asp:CheckBox ID="CheckBox5" CssClass="cb" Text="CheckBox 5" runat="Server" /><br/>

1 Comment

jQuery inside an UpdatePanel??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.