0

I have a datalist inside that I am using a checkbox, I have 1 asp button and 2 image buttton which is outside of datalist something like this

 <asp:DataList ID="dlst1" runat="server" RepeatDirection="Horizontal" OnItemDataBound="dlst1_ItemDataBound" CaptionAlign="Left">
 <ItemTemplate>
       <asp:ImageButton ID="btnImage" runat="server" />
       <asp:CheckBox ID="Chkbox" runat="server"  TextAlign="Right" />
  </ItemTemplate>
</asp:DataList>

<asp:Button ID="Button1" runat="server" Enabled="false" Text="Delete" />
<asp:ImageButton ID="ibtnok" runat="server" Enabled="false" />

I want to enable the Button1 and ibtok when any one checkbox is checked and disable the Button1 and ibtnok when no checkbox is checked.

someone plz help me how to do that with javascript?

2 Answers 2

2

If you are using jquery, you can do it this way:

$("#Chkbox").change(function(){
    if($(this).is(':checked'))
    {
        $('#Button1, #ibtnok').attr('disabled','disabled');
    }
    else
        $('#Button1, #ibtnok').removeAttr('disabled');
})

If there are multiple checkboxes appearing, then you can give those checkboxes a common class, and on every change event, you need to loop through all those elements , or take a count of unchecked/checked checkboxes, and do enable/disable your button.

Looping through each of those checkboxes can be done with $('.your_common_chkbox_class').each(function_to_be_performed);

UPDATE

eg:

$('.your_common_chkbox_class').click(function(){
    if($('.your_common_chkbox_class:checked').length > 0)
        $('#Button1, #ibtnok').attr('disabled','disabled');
    else
        $('#Button1, #ibtnok').removeAttr('disabled');
})
Sign up to request clarification or add additional context in comments.

10 Comments

Totally dig the jquery solution, however in ASP.NET the server-side controls (Chkbox, Button1 and ibtnok) will render with machine generated ids, not the nice clean ids they have in the pre-rendered markup, so that might make it a little hard to provide the correct control ids to the jquery selectors.
@KodeKreachor: thanks for your insights, but in that case, the Id part can be directly specified like $("#<%= Button1.ClientID %>") type of solution. But concept, still would remain the same!
for sure, providing css classes for the selectors to target like you mentioned would be a good way to work around the "id thing" too, +1 for you :)
I am unable to use each with ur example, can u plz give some exp. how to use .each with this
@Rocky : I updated my answer, which could be served as one of the many possible solution.
|
0

Thank you @ linuxeasy I have taken you code and modified (checkbox id) and now its working

<script type="text/javascript" language="javascript">
 $(function () {
    $('.CSSCheck').click(function () {
        if ($("[id$='Chkbox']:checked").length > 0) {
            $("#<%=Button1.ClientID %>").removeAttr('disabled');
        }
        else {
            $("#<%=Button1.ClientID %>").attr('disabled', 'disabled');

        }
    });
});
</script>

1 Comment

Yeah, as I said, I am giving you the idea, and not the exact code, as there are several solutions, you need to evaluate it, as per your need to make it work your way.

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.