Disable Button on CheckBox unchecked

4 posts, 1 answers
  1. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 15 Oct 2013 Link to this post

    I am using the code below to force customers to select item from a Grid CheckBox
    function GetCheckedItemCountbtnClick() {
                var checkedcount = 0;
                var grid = $find("<%=gvSummary.ClientID %>");           
                for (var i = 0; i < grid.get_masterTableView().get_dataItems().length; i++) {
                    var objchkbox = grid.get_masterTableView().get_dataItems()[i].findElement("CheckBoxStatus");
                     if (objchkbox.checked) {
                        checkedcount++;
                    }
                     
                    //}
                    if (checkedcount > 8) {
                         objchkbox.checked = false;
                      
                        alert("Please select only 8 item");
                        return false;                  
     
                    }
                }
                if (checkedcount == 0) {
                      
                    alert("Please Check at least one CheckBox");
                     
                    return false;
     
                }
     
             
            }


    I have a Button I want to disable when the 'checkedcount ==0' unless an item is checked
    <asp:Button ID="btnPrint" runat="server" Font-Size="X-Small" OnClick="btnPrint_Click"
                                Text="Print" ToolTip="Create a  report..." />

    Nothing seems to work for me. Any idea will be appreciated.





  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Oct 2013 Link to this post

    Hi Ebenezer,

    I guess you are trying to disable the Button 'btnPrint' on another Button Click. Please have a look into the following code snippet that I tried.

    ASPX:
    <telerik:RadButton ID="RadButton1" runat="server" Text="CheckedItems" OnClientClicked="GetCheckedItemCountbtnClick"
        AutoPostBack="false">
    </telerik:RadButton>
    <br />
    <asp:Button ID="btnPrint" runat="server" Font-Size="X-Small" Text="Print" ToolTip="Create a  report..." />

    JavaScript:
    <script type="text/javascript">
        function GetCheckedItemCountbtnClick(sender, args) {
            var checkedcount = 0;
            var grid = $find("<%=gvSummary.ClientID %>");
            for (var i = 0; i < grid.get_masterTableView().get_dataItems().length; i++) {
                var objchkbox = grid.get_masterTableView().get_dataItems()[i].findElement("CheckBoxStatus");
                if (objchkbox.checked) {
                    checkedcount++;
                }
                if (checkedcount > 8) {
                    objchkbox.checked = false;
                    alert("Please select only 8 item");
                    return false;
                }
            }
            if (checkedcount == 0) {
                alert("Please Check at least one CheckBox");
                var button = document.getElementById("btnPrint");//accessing the asp button
                button.disabled = true;//disabling the button
                return false;
            }
        }
    </script>

    Thanks,
    Shinu.

  3. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 16 Oct 2013 Link to this post

    Hi Shinu
    Thanks for your response but I am getting an error
    Error: Unable to set value of the property 'disabled': object is null or undefined
  4. Ebenezer
    Ebenezer avatar
    39 posts
    Member since:
    Dec 2012

    Posted 16 Oct 2013 Link to this post

    Solved. I had to change
    if (checkedcount == 0) {
                    alert("Please Check at least one CheckBox");
                    var button = document.getElementById("<%=btnPrint.ClientID %>"); //accessing the asp button       
                    button.disabled = true; //disabling the button
                    return false;
                }
Back to Top